Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 1 addition & 4 deletions language/control-structures/continue.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -103,10 +101,9 @@ Inner
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
continue;
print "$i\n";
}
?>
]]>
</programlisting>
<para>
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
20 changes: 16 additions & 4 deletions language/control-structures/else.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@
<programlisting role="php">
<![CDATA[
<?php
$a = 3;
$b = 5;
if ($a > $b) {
echo "a is greater than b";
echo "a is greater than b\n";
} else {
echo "a is NOT greater than b";
echo "a is NOT greater than b\n";
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
a is NOT greater than b
]]>
</screen>
</informalexample>

The <literal>else</literal> statement is only executed if the
Expand All @@ -53,9 +60,14 @@ if ($a)
echo "b";
else
echo "c";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[

]]>
</screen>
</informalexample>
Despite the indentation (which does not matter for PHP), the <literal>else</literal>
is associated with the <literal>if ($b)</literal>, so the example does not produce
Expand Down
Loading