Skip to content
2 changes: 1 addition & 1 deletion language/control-structures.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="language.control-structures" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<chapter xml:id="language.control-structures" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" annotations="interactive">
<title>Control Structures</title>

<sect1 xml:id="control-structures.intro">
Expand Down
42 changes: 36 additions & 6 deletions language/control-structures/alternative-syntax.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
<informalexample>
<programlisting role="php">
<![CDATA[
<?php if ($a == 5): ?>
<?php
$a = 5;
if ($a == 5): ?>
A is equal to 5
<?php endif; ?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
A is equal to 5
]]>
</screen>
</informalexample>
</para>
<simpara>
Expand All @@ -38,6 +46,7 @@ A is equal to 5
<programlisting role="php">
<![CDATA[
<?php
$a = 5;
if ($a == 5):
echo "a equals 5";
echo "...";
Expand All @@ -47,9 +56,14 @@ elseif ($a == 6):
else:
echo "a is neither 5 nor 6";
endif;
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
a equals 5...
]]>
</screen>
</informalexample>
</para>
<note>
Expand All @@ -66,12 +80,20 @@ endif;
<informalexample>
<programlisting role="php">
<![CDATA[
<?php switch ($foo): ?>
<?php
$foo = 1;
switch ($foo): ?>
<?php case 1: ?>
// ...
Foo is 1
<?php endswitch; ?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Parse error: syntax error, unexpected T_INLINE_HTML " ", expecting "endswitch" or "case" or "default" in script on line 4
]]>
</screen>
</informalexample>
<para>
Whereas this is valid, as the trailing newline after the
Expand All @@ -82,12 +104,20 @@ endif;
<informalexample>
<programlisting role="php">
<![CDATA[
<?php switch ($foo): ?>
<?php
$foo = 1;
switch ($foo): ?>
<?php case 1: ?>
...
Foo is 1
<?php endswitch; ?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Foo is 1
]]>
</screen>
</informalexample>
</warning>
<para>
Expand Down
48 changes: 41 additions & 7 deletions language/control-structures/break.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,61 @@ foreach ($arr as $val) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
echo "$val\n";
}

/* Using the optional argument. */

]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
one
two
three
four
]]>
</screen>
</informalexample>
</para>
<simpara>
Using the optional argument:
</simpara>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
echo "At 5\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />\n";
echo "At 10; quitting\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
echo "$i\n";
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
1
2
3
4
At 5
5
6
7
8
9
At 10; quitting
]]>
</screen>
</informalexample>
</para>

Expand Down
36 changes: 2 additions & 34 deletions language/control-structures/continue.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
considered a looping structure for the purposes of
<literal>continue</literal>. <literal>continue</literal> behaves like
<literal>break</literal> (when no arguments are passed) but will
raise a warning as this is likely to be a mistake. If a
raise a warning as this is likely to be a mistake. If a
<literal>switch</literal> is inside a loop,
<literal>continue 2</literal> will continue with the next iteration
of the outer loop.
Expand All @@ -40,7 +40,6 @@ foreach ($arr as $key => $value) {
}
echo $value . "\n";
}
?>
]]>
</programlisting>
&examples.outputs;
Expand All @@ -67,7 +66,6 @@ while ($i++ < 5) {
}
echo "Neither does this.\n";
}
?>
]]>
</programlisting>
&examples.outputs;
Expand All @@ -88,36 +86,6 @@ Inner
Outer
Middle
Inner
]]>
</screen>
</informalexample>
</para>
<para>
Omitting the semicolon after <literal>continue</literal> can lead to
confusion. Here's an example of what you shouldn't do.
</para>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i\n";
}
?>
]]>
</programlisting>
<para>
One can expect the result to be:
</para>
<screen>
<![CDATA[
0
1
3
4
]]>
</screen>
</informalexample>
Expand All @@ -136,7 +104,7 @@ for ($i = 0; $i < 5; ++$i) {
<row>
<entry>7.3.0</entry>
<entry>
<literal>continue</literal> within a <literal>switch</literal> that is attempting to act like a <literal>break</literal> statement for the
<literal>continue</literal> within a <literal>switch</literal> that is attempting to act like a <literal>break</literal> statement for the
<literal>switch</literal> will trigger an <constant>E_WARNING</constant>.
</entry>
</row>
Expand Down
23 changes: 14 additions & 9 deletions language/control-structures/declare.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ declare (directive)
be given as directive values. Variables and constants cannot be used. To
illustrate:
<informalexample>
<programlisting role="php">
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
// This is valid:
Expand All @@ -45,7 +45,6 @@ declare(ticks=1);
// This is invalid:
const TICK_VALUE = 1;
declare(ticks=TICK_VALUE);
?>
]]>
</programlisting>
</informalexample>
Expand All @@ -63,7 +62,7 @@ declare(ticks=TICK_VALUE);
<literal>declare</literal> was included then it does not affect the parent
file).
<informalexample>
<programlisting role="php">
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
// these are the same:
Expand All @@ -76,7 +75,6 @@ declare(ticks=1) {
// or you can use this:
declare(ticks=1);
// entire script here
?>
]]>
</programlisting>
</informalexample>
Expand Down Expand Up @@ -123,12 +121,20 @@ $a = 1; // causes a tick event

if ($a > 0) {
$a += 2; // causes a tick event
print $a; // causes a tick event
print $a . "\n"; // causes a tick event
}

?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
tick_handler() called
tick_handler() called
tick_handler() called
3
tick_handler() called
]]>
</screen>
</example>
</para>
<simpara>
Expand All @@ -142,12 +148,11 @@ if ($a > 0) {
A script's encoding can be specified per-script using the <literal>encoding</literal> directive.
<example>
<title>Declaring an encoding for the script</title>
<programlisting role="php">
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
declare(encoding='ISO-8859-1');
// code here
?>
]]>
</programlisting>
</example>
Expand Down
22 changes: 18 additions & 4 deletions language/control-structures/do-while.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ $i = 0;
do {
echo $i;
} while ($i > 0);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
0
]]>
</screen>
</informalexample>
</para>
<simpara>
Expand All @@ -50,23 +55,32 @@ do {
<programlisting role="php">
<![CDATA[
<?php
$i = 50;
$factor = 0.5;
$minimum_limit = 20;
do {
if ($i < 5) {
echo "i is not big enough";
echo "i is not big enough\n";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
echo "i is less than minimum limit after factor\n";
break;
}
echo "i is ok";
echo $i ." is ok\n";

/* process i */

} while (0);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
25 is ok
]]>
</screen>
</informalexample>
</para>
<simpara>
Expand Down
Loading