@@ -105,16 +105,26 @@ echo implode(" and ", $fruits); // lemon and orange and banana
105
105
// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
106
106
echo 6 * 7; // 42
107
107
108
- // Because echo does not behave as an expression, the following code is invalid.
109
- ($some_var) ? echo 'true' : echo 'false';
110
-
111
108
// However, the following examples will work:
112
109
($some_var) ? print 'true' : print 'false'; // print is also a construct, but
113
110
// it is a valid expression, returning 1,
114
111
// so it may be used in this context.
115
112
116
113
echo $some_var ? 'true': 'false'; // evaluating the expression first and passing it to echo
117
114
?>
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
+ ?>
118
128
]]>
119
129
</programlisting >
120
130
</example >
@@ -134,32 +144,44 @@ echo $some_var ? 'true': 'false'; // evaluating the expression first and passing
134
144
part of the expression being output, not part of the <literal >echo</literal >
135
145
syntax itself.
136
146
137
- <informalexample >
147
+ <example >
148
+ <title >Using Parentheses</title >
138
149
<programlisting role =" php" >
139
150
<![CDATA[
140
151
<?php
141
- echo "hello";
152
+ echo "hello", PHP_EOL ;
142
153
// outputs "hello"
143
154
144
- echo("hello");
155
+ echo("hello"), PHP_EOL ;
145
156
// also outputs "hello", because ("hello") is a valid expression
146
157
147
- echo(1 + 2) * 3;
158
+ echo(1 + 2) * 3, PHP_EOL ;
148
159
// outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3
149
160
// the echo statement sees the whole expression as one argument
150
161
151
- echo "hello", " world";
162
+ echo "hello", " world", PHP_EOL ;
152
163
// outputs "hello world"
153
164
154
- echo("hello"), (" world");
165
+ echo("hello"), (" world"), PHP_EOL ;
155
166
// outputs "hello world"; the parentheses are part of each expression
167
+ ?>
168
+ ]]>
169
+ </programlisting >
170
+ </example >
171
+ </para >
156
172
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;
158
180
// Throws a Parse Error because ("hello", " world") is not a valid expression
159
181
?>
160
182
]]>
161
183
</programlisting >
162
- </informalexample >
184
+ </example >
163
185
</para >
164
186
</note >
165
187
0 commit comments