Skip to content

Commit b8147b7

Browse files
AllenJBlacatoire
andauthored
Enable runnable (WASM) examples for language.control-structures section (#5462)
* Enable runnable (WASM) examples for language.control-structures section Co-authored-by: Louis-Arnaud <la.catoire@gmail.com>
1 parent 339ba3f commit b8147b7

16 files changed

Lines changed: 496 additions & 182 deletions

language/control-structures.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<chapter xml:id="language.control-structures" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
3+
<chapter xml:id="language.control-structures" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" annotations="interactive">
44
<title>Control Structures</title>
55

66
<sect1 xml:id="control-structures.intro">

language/control-structures/alternative-syntax.xml

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@
1717
<informalexample>
1818
<programlisting role="php">
1919
<![CDATA[
20-
<?php if ($a == 5): ?>
20+
<?php
21+
$a = 5;
22+
if ($a == 5): ?>
2123
A is equal to 5
2224
<?php endif; ?>
2325
]]>
2426
</programlisting>
27+
&example.outputs;
28+
<screen>
29+
<![CDATA[
30+
A is equal to 5
31+
]]>
32+
</screen>
2533
</informalexample>
2634
</para>
2735
<simpara>
@@ -38,6 +46,7 @@ A is equal to 5
3846
<programlisting role="php">
3947
<![CDATA[
4048
<?php
49+
$a = 5;
4150
if ($a == 5):
4251
echo "a equals 5";
4352
echo "...";
@@ -47,9 +56,14 @@ elseif ($a == 6):
4756
else:
4857
echo "a is neither 5 nor 6";
4958
endif;
50-
?>
5159
]]>
5260
</programlisting>
61+
&example.outputs;
62+
<screen>
63+
<![CDATA[
64+
a equals 5...
65+
]]>
66+
</screen>
5367
</informalexample>
5468
</para>
5569
<note>
@@ -66,12 +80,20 @@ endif;
6680
<informalexample>
6781
<programlisting role="php">
6882
<![CDATA[
69-
<?php switch ($foo): ?>
83+
<?php
84+
$foo = 1;
85+
switch ($foo): ?>
7086
<?php case 1: ?>
71-
// ...
87+
Foo is 1
7288
<?php endswitch; ?>
7389
]]>
7490
</programlisting>
91+
&example.outputs;
92+
<screen>
93+
<![CDATA[
94+
Parse error: syntax error, unexpected T_INLINE_HTML " ", expecting "endswitch" or "case" or "default" in script on line 4
95+
]]>
96+
</screen>
7597
</informalexample>
7698
<para>
7799
Whereas this is valid, as the trailing newline after the
@@ -82,12 +104,20 @@ endif;
82104
<informalexample>
83105
<programlisting role="php">
84106
<![CDATA[
85-
<?php switch ($foo): ?>
107+
<?php
108+
$foo = 1;
109+
switch ($foo): ?>
86110
<?php case 1: ?>
87-
...
111+
Foo is 1
88112
<?php endswitch; ?>
89113
]]>
90114
</programlisting>
115+
&example.outputs;
116+
<screen>
117+
<![CDATA[
118+
Foo is 1
119+
]]>
120+
</screen>
91121
</informalexample>
92122
</warning>
93123
<para>

language/control-structures/break.xml

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,61 @@ foreach ($arr as $val) {
2626
if ($val == 'stop') {
2727
break; /* You could also write 'break 1;' here. */
2828
}
29-
echo "$val<br />\n";
29+
echo "$val\n";
3030
}
31-
32-
/* Using the optional argument. */
33-
31+
]]>
32+
</programlisting>
33+
&example.outputs;
34+
<screen>
35+
<![CDATA[
36+
one
37+
two
38+
three
39+
four
40+
]]>
41+
</screen>
42+
</informalexample>
43+
</para>
44+
<simpara>
45+
Using the optional argument:
46+
</simpara>
47+
<para>
48+
<informalexample>
49+
<programlisting role="php">
50+
<![CDATA[
51+
<?php
3452
$i = 0;
3553
while (++$i) {
3654
switch ($i) {
3755
case 5:
38-
echo "At 5<br />\n";
56+
echo "At 5\n";
3957
break 1; /* Exit only the switch. */
4058
case 10:
41-
echo "At 10; quitting<br />\n";
59+
echo "At 10; quitting\n";
4260
break 2; /* Exit the switch and the while. */
4361
default:
4462
break;
4563
}
64+
echo "$i\n";
4665
}
47-
?>
4866
]]>
4967
</programlisting>
68+
&example.outputs;
69+
<screen>
70+
<![CDATA[
71+
1
72+
2
73+
3
74+
4
75+
At 5
76+
5
77+
6
78+
7
79+
8
80+
9
81+
At 10; quitting
82+
]]>
83+
</screen>
5084
</informalexample>
5185
</para>
5286

language/control-structures/continue.xml

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
considered a looping structure for the purposes of
1717
<literal>continue</literal>. <literal>continue</literal> behaves like
1818
<literal>break</literal> (when no arguments are passed) but will
19-
raise a warning as this is likely to be a mistake. If a
19+
raise a warning as this is likely to be a mistake. If a
2020
<literal>switch</literal> is inside a loop,
2121
<literal>continue 2</literal> will continue with the next iteration
2222
of the outer loop.
@@ -40,7 +40,6 @@ foreach ($arr as $key => $value) {
4040
}
4141
echo $value . "\n";
4242
}
43-
?>
4443
]]>
4544
</programlisting>
4645
&examples.outputs;
@@ -67,7 +66,6 @@ while ($i++ < 5) {
6766
}
6867
echo "Neither does this.\n";
6968
}
70-
?>
7169
]]>
7270
</programlisting>
7371
&examples.outputs;
@@ -88,36 +86,6 @@ Inner
8886
Outer
8987
Middle
9088
Inner
91-
]]>
92-
</screen>
93-
</informalexample>
94-
</para>
95-
<para>
96-
Omitting the semicolon after <literal>continue</literal> can lead to
97-
confusion. Here's an example of what you shouldn't do.
98-
</para>
99-
<para>
100-
<informalexample>
101-
<programlisting role="php">
102-
<![CDATA[
103-
<?php
104-
for ($i = 0; $i < 5; ++$i) {
105-
if ($i == 2)
106-
continue
107-
print "$i\n";
108-
}
109-
?>
110-
]]>
111-
</programlisting>
112-
<para>
113-
One can expect the result to be:
114-
</para>
115-
<screen>
116-
<![CDATA[
117-
0
118-
1
119-
3
120-
4
12189
]]>
12290
</screen>
12391
</informalexample>
@@ -136,7 +104,7 @@ for ($i = 0; $i < 5; ++$i) {
136104
<row>
137105
<entry>7.3.0</entry>
138106
<entry>
139-
<literal>continue</literal> within a <literal>switch</literal> that is attempting to act like a <literal>break</literal> statement for the
107+
<literal>continue</literal> within a <literal>switch</literal> that is attempting to act like a <literal>break</literal> statement for the
140108
<literal>switch</literal> will trigger an <constant>E_WARNING</constant>.
141109
</entry>
142110
</row>

language/control-structures/declare.xml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ declare (directive)
3636
be given as directive values. Variables and constants cannot be used. To
3737
illustrate:
3838
<informalexample>
39-
<programlisting role="php">
39+
<programlisting role="php" annotations="non-interactive">
4040
<![CDATA[
4141
<?php
4242
// This is valid:
@@ -45,7 +45,6 @@ declare(ticks=1);
4545
// This is invalid:
4646
const TICK_VALUE = 1;
4747
declare(ticks=TICK_VALUE);
48-
?>
4948
]]>
5049
</programlisting>
5150
</informalexample>
@@ -63,7 +62,7 @@ declare(ticks=TICK_VALUE);
6362
<literal>declare</literal> was included then it does not affect the parent
6463
file).
6564
<informalexample>
66-
<programlisting role="php">
65+
<programlisting role="php" annotations="non-interactive">
6766
<![CDATA[
6867
<?php
6968
// these are the same:
@@ -76,7 +75,6 @@ declare(ticks=1) {
7675
// or you can use this:
7776
declare(ticks=1);
7877
// entire script here
79-
?>
8078
]]>
8179
</programlisting>
8280
</informalexample>
@@ -123,12 +121,20 @@ $a = 1; // causes a tick event
123121
124122
if ($a > 0) {
125123
$a += 2; // causes a tick event
126-
print $a; // causes a tick event
124+
print $a . "\n"; // causes a tick event
127125
}
128-
129-
?>
130126
]]>
131127
</programlisting>
128+
&example.outputs;
129+
<screen>
130+
<![CDATA[
131+
tick_handler() called
132+
tick_handler() called
133+
tick_handler() called
134+
3
135+
tick_handler() called
136+
]]>
137+
</screen>
132138
</example>
133139
</para>
134140
<simpara>
@@ -142,12 +148,11 @@ if ($a > 0) {
142148
A script's encoding can be specified per-script using the <literal>encoding</literal> directive.
143149
<example>
144150
<title>Declaring an encoding for the script</title>
145-
<programlisting role="php">
151+
<programlisting role="php" annotations="non-interactive">
146152
<![CDATA[
147153
<?php
148154
declare(encoding='ISO-8859-1');
149155
// code here
150-
?>
151156
]]>
152157
</programlisting>
153158
</example>

language/control-structures/do-while.xml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ $i = 0;
2828
do {
2929
echo $i;
3030
} while ($i > 0);
31-
?>
3231
]]>
3332
</programlisting>
33+
&example.outputs;
34+
<screen>
35+
<![CDATA[
36+
0
37+
]]>
38+
</screen>
3439
</informalexample>
3540
</para>
3641
<simpara>
@@ -50,23 +55,32 @@ do {
5055
<programlisting role="php">
5156
<![CDATA[
5257
<?php
58+
$i = 50;
59+
$factor = 0.5;
60+
$minimum_limit = 20;
5361
do {
5462
if ($i < 5) {
55-
echo "i is not big enough";
63+
echo "i is not big enough\n";
5664
break;
5765
}
5866
$i *= $factor;
5967
if ($i < $minimum_limit) {
68+
echo "i is less than minimum limit after factor\n";
6069
break;
6170
}
62-
echo "i is ok";
71+
echo $i ." is ok\n";
6372
6473
/* process i */
6574
6675
} while (0);
67-
?>
6876
]]>
6977
</programlisting>
78+
&example.outputs;
79+
<screen>
80+
<![CDATA[
81+
25 is ok
82+
]]>
83+
</screen>
7084
</informalexample>
7185
</para>
7286
<simpara>

0 commit comments

Comments
 (0)