Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
18 changes: 12 additions & 6 deletions language/control-structures/alternative-syntax.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
<informalexample>
<programlisting role="php">
<![CDATA[
<?php if ($a == 5): ?>
<?php
$a = 5;
if ($a == 5): ?>
A is equal to 5
<?php endif; ?>
]]>
Expand All @@ -38,6 +40,7 @@ A is equal to 5
<programlisting role="php">
<![CDATA[
<?php
$a = 5;
if ($a == 5):
echo "a equals 5";
echo "...";
Expand All @@ -47,7 +50,6 @@ elseif ($a == 6):
else:
echo "a is neither 5 nor 6";
endif;
?>
]]>
</programlisting>
</informalexample>
Expand All @@ -66,9 +68,11 @@ endif;
<informalexample>
<programlisting role="php">
<![CDATA[
<?php switch ($foo): ?>
<?php
$foo = 1;
switch ($foo): ?>
<?php case 1: ?>
// ...
Foo is 1
<?php endswitch; ?>
]]>
</programlisting>
Expand All @@ -82,9 +86,11 @@ endif;
<informalexample>
<programlisting role="php">
<![CDATA[
<?php switch ($foo): ?>
<?php
$foo = 1;
switch ($foo): ?>
<?php case 1: ?>
...
Foo is 1
<?php endswitch; ?>
]]>
</programlisting>
Expand Down
11 changes: 5 additions & 6 deletions language/control-structures/break.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,24 @@ 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. */

echo "\nUsing the optional argument:\n";
Comment thread
AllenJB marked this conversation as resolved.
Outdated
$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>
</informalexample>
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
13 changes: 4 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,10 +121,8 @@ $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>
Expand All @@ -142,12 +138,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
10 changes: 6 additions & 4 deletions language/control-structures/do-while.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ $i = 0;
do {
echo $i;
} while ($i > 0);
?>
]]>
</programlisting>
</informalexample>
Expand All @@ -50,21 +49,24 @@ 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>
</informalexample>
Expand Down
8 changes: 4 additions & 4 deletions language/control-structures/else.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
<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>
</informalexample>
Expand Down Expand Up @@ -53,7 +54,6 @@ if ($a)
echo "b";
else
echo "c";
?>
]]>
</programlisting>
</informalexample>
Expand Down
10 changes: 7 additions & 3 deletions language/control-structures/elseif.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
<programlisting role="php">
<![CDATA[
<?php
$a = 1;
$b = 1;
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
]]>
</programlisting>
</informalexample>
Expand Down Expand Up @@ -67,6 +68,8 @@ if ($a > $b) {
<programlisting role="php">
<![CDATA[
<?php
$a = 3;
$b = 1;

/* Incorrect Method: */
if ($a > $b):
Expand All @@ -82,6 +85,9 @@ endif;
<programlisting role="php">
<![CDATA[
<?php
$a = 3;
$b = 1;

/* Correct Method: */
if ($a > $b):
echo $a." is greater than ".$b;
Expand All @@ -90,8 +96,6 @@ elseif ($a == $b): // Note the combination of the words.
else:
echo $a." is neither greater than or equal to ".$b;
endif;

?>
]]>
</programlisting>
</informalexample>
Expand Down
29 changes: 12 additions & 17 deletions language/control-structures/for.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,31 @@ for (expr1; expr2; expr3)
<programlisting role="php">
<![CDATA[
<?php
/* example 1 */

echo "Example 1:\n";
for ($i = 1; $i <= 10; $i++) {
echo $i;
echo $i . " ";
}

/* example 2 */

echo "\nExample 2:\n";
Comment thread
AllenJB marked this conversation as resolved.
Outdated
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
echo $i . " ";
}

/* example 3 */

echo "\nExample 3:\n";
$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
echo $i . " ";
$i++;
}

/* example 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
echo "\nExample 4:\n";
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i . " ", $i++);
]]>
</programlisting>
</informalexample>
Expand Down Expand Up @@ -127,9 +122,9 @@ $people = array(
);

for($i = 0; $i < count($people); ++$i) {
$people[$i]['salt'] = mt_rand(000000, 999999);
$people[$i]['salt'] = random_int(100000, 999999);
Comment thread
AllenJB marked this conversation as resolved.
}
?>
var_dump($people);
]]>
</programlisting>
</informalexample>
Expand All @@ -151,9 +146,9 @@ $people = array(
);

for($i = 0, $size = count($people); $i < $size; ++$i) {
$people[$i]['salt'] = mt_rand(000000, 999999);
$people[$i]['salt'] = random_int(100000, 999999);
}
?>
var_dump($people);
]]>
</programlisting>
</informalexample>
Expand Down
Loading