@@ -105,16 +105,26 @@ echo implode(" and ", $fruits); // lemon and orange and banana
105105// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
106106echo 6 * 7; // 42
107107
108- // Because echo does not behave as an expression, the following code is invalid.
109- ($some_var) ? echo 'true' : echo 'false';
110-
111108// However, the following examples will work:
112109($some_var) ? print 'true' : print 'false'; // print is also a construct, but
113110 // it is a valid expression, returning 1,
114111 // so it may be used in this context.
115112
116113echo $some_var ? 'true': 'false'; // evaluating the expression first and passing it to echo
117114?>
115+ ]]>
116+ </programlisting >
117+ </example >
118+ </para >
119+ <para >
120+ <example >
121+ <title ><literal >echo</literal > is not an expression</title >
122+ <programlisting role =" php" annotations =" non-interactive" >
123+ <![CDATA[
124+ <?php
125+ // Because echo does not behave as an expression, the following code is invalid.
126+ ($some_var) ? echo 'true' : echo 'false';
127+ ?>
118128]]>
119129 </programlisting >
120130 </example >
@@ -134,32 +144,44 @@ echo $some_var ? 'true': 'false'; // evaluating the expression first and passing
134144 part of the expression being output, not part of the <literal >echo</literal >
135145 syntax itself.
136146
137- <informalexample >
147+ <example >
148+ <title >Using Parentheses</title >
138149 <programlisting role =" php" >
139150 <![CDATA[
140151<?php
141- echo "hello";
152+ echo "hello", PHP_EOL ;
142153// outputs "hello"
143154
144- echo("hello");
155+ echo("hello"), PHP_EOL ;
145156// also outputs "hello", because ("hello") is a valid expression
146157
147- echo(1 + 2) * 3;
158+ echo(1 + 2) * 3, PHP_EOL ;
148159// outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3
149160// the echo statement sees the whole expression as one argument
150161
151- echo "hello", " world";
162+ echo "hello", " world", PHP_EOL ;
152163// outputs "hello world"
153164
154- echo("hello"), (" world");
165+ echo("hello"), (" world"), PHP_EOL ;
155166// outputs "hello world"; the parentheses are part of each expression
167+ ?>
168+ ]]>
169+ </programlisting >
170+ </example >
171+ </para >
156172
157- echo("hello", " world");
173+ <para >
174+ <example >
175+ <title >Invalid Expression</title >
176+ <programlisting role =" php" annotations =" non-interactive" >
177+ <![CDATA[
178+ <?php
179+ echo("hello", " world"), PHP_EOL;
158180// Throws a Parse Error because ("hello", " world") is not a valid expression
159181?>
160182]]>
161183 </programlisting >
162- </informalexample >
184+ </example >
163185 </para >
164186 </note >
165187
0 commit comments