reference/strings/functions/echo.xml
7a3899eea90f3df8dcfe8fd350900162f3490bed
...
...
@@ -10,27 +10,23 @@
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
12
<type>void</type><methodname>echo</methodname>
13
-
<methodparam><type>string</type><parameter>arg1</parameter></methodparam>
14
-
<methodparam choice="opt"><type>string</type><parameter>...</parameter></methodparam>
13
+
<methodparam rep="repeat"><type>string</type><parameter>expressions</parameter></methodparam>
15
14
</methodsynopsis>
16
15
<simpara>
17
-
Outputs all parameters.
16
+
Outputs one or more expressions, with no additional newlines or spaces.
18
17
</simpara>
19
18
<para>
20
-
<function>echo</function> is not actually a function (it is a
21
-
language construct), so you are not required to use parentheses
22
-
with it. <function>echo</function> (unlike some other language
23
-
constructs) does not behave like a function, so it cannot
24
-
always be used in the context of a function. Additionally, if you want to
25
-
pass more than one parameter to <function>echo</function>, the parameters
26
-
must not be enclosed within parentheses.
19
+
<literal>echo</literal> is not a function but a language construct.
20
+
Its arguments are a list of expressions following the <literal>echo</literal>
21
+
keyword, separated by commas, and not delimited by parentheses.
22
+
Unlike some other language constructs, <literal>echo</literal> does not have
23
+
any return value, so it cannot be used in the context of an expression.
27
24
</para>
28
25
<para>
29
-
<function>echo</function> also has a shortcut syntax, where you can
30
-
immediately follow the opening tag with an equals sign. This short syntax
31
-
only works with the <link
32
-
linkend="ini.short-open-tag">short_open_tag</link> configuration setting
33
-
enabled.
26
+
<literal>echo</literal> also has a shortcut syntax, where you can
27
+
immediately follow the opening tag with an equals sign. This syntax is available
28
+
even with the <link linkend="ini.short-open-tag">short_open_tag</link> configuration
29
+
setting disabled.
34
30
<informalexample>
35
31
<programlisting role="php">
36
32
<![CDATA[
...
...
@@ -39,6 +35,10 @@ I have <?=$foo?> foo.
39
35
</programlisting>
40
36
</informalexample>
41
37
</para>
38
+
<para>
39
+
The major differences to <function>print</function> are that
40
+
<literal>echo</literal> accepts multiple arguments and doesn't have a return value.
41
+
</para>
42
42
</refsect1>
43
43

44
44
<refsect1 role="parameters">
...
...
@@ -46,17 +46,13 @@ I have <?=$foo?> foo.
46
46
<para>
47
47
<variablelist>
48
48
<varlistentry>
49
-
<term><parameter>arg1</parameter></term>
50
-
<listitem>
51
-
<para>
52
-
The parameter to output.
53
-
</para>
54
-
</listitem>
55
-
</varlistentry>
56
-
<varlistentry>
57
-
<term><parameter>...</parameter></term>
49
+
<term><parameter>expressions</parameter></term>
58
50
<listitem>
59
51
<para>
52
+
One or more string expressions to output, separated by commas.
53
+
Non-string values will be coerced to strings, even when
54
+
<link linkend="language.types.declarations.strict">the
55
+
<literal>strict_types</literal> directive</link> is enabled.
60
56
</para>
61
57
</listitem>
62
58
</varlistentry>
...
...
@@ -75,57 +71,49 @@ I have <?=$foo?> foo.
75
71
&reftitle.examples;
76
72
<para>
77
73
<example>
78
-
<title><function>echo</function> examples</title>
74
+
<title><literal>echo</literal> examples</title>
79
75
<programlisting role="php">
80
76
<![CDATA[
81
77
<?php
82
-
echo "Hello World";
83
-

84
-
echo "This spans
85
-
multiple lines. The newlines will be
86
-
output as well";
87
-

88
-
echo "This spans\nmultiple lines. The newlines will be\noutput as well.";
89
-

90
-
echo "Escaping characters is done \"Like this\".";
78
+
echo "echo does not require parentheses.";
91
79

92
-
// You can use variables inside of an echo statement
93
-
$foo = "foobar";
94
-
$bar = "barbaz";
80
+
// Strings can either be passed individually as multiple arguments or
81
+
// concatenated together and passed as a single argument
82
+
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', "\n";
83
+
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
95
84

96
-
echo "foo is $foo"; // foo is foobar
85
+
// No newline or space is added; the below outputs "helloworld" all on one line
86
+
echo "hello";
87
+
echo "world";
97
88

98
-
// You can also use arrays
99
-
$baz = array("value" => "foo");
89
+
// Same as above
90
+
echo "hello", "world";
100
91

101
-
echo "this is {$baz['value']} !"; // this is foo !
92
+
echo "This string spans
93
+
multiple lines. The newlines will be
94
+
output as well";
102
95

103
-
// Using single quotes will print the variable name, not the value
104
-
echo 'foo is $foo'; // foo is $foo
96
+
echo "This string spans\nmultiple lines. The newlines will be\noutput as well.";
105
97

106
-
// If you are not using any other characters, you can just echo variables
107
-
echo $foo; // foobar
108
-
echo $foo,$bar; // foobarbarbaz
98
+
// The argument can be any expression which produces a string
99
+
$foo = "example";
100
+
echo "foo is $foo"; // foo is example
109
101

110
-
// Some people prefer passing multiple parameters to echo over concatenation.
111
-
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
112
-
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
102
+
$fruits = ["lemon", "orange", "banana"];
103
+
echo implode(" and ", $fruits); // lemon and orange and banana
113
104

114
-
echo <<<END
115
-
This uses the "here document" syntax to output
116
-
multiple lines with $variable interpolation. Note
117
-
that the here document terminator must appear on a
118
-
line with just a semicolon. no extra whitespace!
119
-
END;
105
+
// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
106
+
echo 6 * 7; // 42
120
107

121
-
// Because echo does not behave like a function, the following code is invalid.
108
+
// Because echo does not behave as an expression, the following code is invalid.
122
109
($some_var) ? echo 'true' : echo 'false';
123
110

124
111
// However, the following examples will work:
125
112
($some_var) ? print 'true' : print 'false'; // print is also a construct, but
126
-
// it behaves like a function, so
127
-
// it may be used in this context.
128
-
echo $some_var ? 'true': 'false'; // changing the statement around
113
+
// it is a valid expression, returning 1,
114
+
// so it may be used in this context.
115
+

116
+
echo $some_var ? 'true': 'false'; // evaluating the expression first and passing it to echo
129
117
?>
130
118
]]>
131
119
</programlisting>
...
...
@@ -136,6 +124,85 @@ echo $some_var ? 'true': 'false'; // changing the statement around
136
124
<refsect1 role="notes">
137
125
&reftitle.notes;
138
126
&note.language-construct;
127
+

128
+
<note>
129
+
<title>Using with parentheses</title>
130
+
<para>
131
+
Surrounding a single argument to <literal>echo</literal> with parentheses will not
132
+
raise a syntax error, and produces syntax which looks like a normal
133
+
function call. However, this can be misleading, because the parentheses are actually
134
+
part of the expression being output, not part of the <literal>echo</literal>
135
+
syntax itself.
136
+
</para>
137
+
<para>
138
+
<example>
139
+
<title/>
140
+
<programlisting role="php">
141
+
<![CDATA[
142
+
<?php
143
+
echo "hello";
144
+
// outputs "hello"
145
+

146
+
echo("hello");
147
+
// also outputs "hello", because ("hello") is a valid expression
148
+

149
+
echo(1 + 2) * 3;
150
+
// outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3
151
+
// the echo statement sees the whole expression as one argument
152
+

153
+
echo "hello", " world";
154
+
// outputs "hello world"
155
+

156
+
echo("hello"), (" world");
157
+
// outputs "hello world"; the parentheses are part of each expression
158
+

159
+
echo("hello", " world");
160
+
// Throws a Parse Error because ("hello", " world") is not a valid expression
161
+
?>
162
+
]]>
163
+
</programlisting>
164
+
</example>
165
+
</para>
166
+
</note>
167
+

168
+
<tip>
169
+
<para>
170
+
Passing multiple arguments to <literal>echo</literal> can avoid
171
+
complications arising from the precedence of the concatenation operator in
172
+
PHP. For instance, the concatenation operator has higher precedence than
173
+
the ternary operator, and prior to PHP 8.0.0 had the same precedence as addition
174
+
and subtraction:
175
+
</para>
176
+
<programlisting role="php">
177
+
<![CDATA[
178
+
<?php
179
+
// Below, the expression 'Hello ' . isset($name) is evaluated first,
180
+
// and is always true, so the argument to echo is always $name
181
+
echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';
182
+

183
+
// The intended behaviour requires additional parentheses
184
+
echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';
185
+

186
+
// In PHP prior to 8.0.0, the below outputs "2", rather than "Sum: 3"
187
+
echo 'Sum: ' . 1 + 2;
188
+

189
+
// Again, adding parentheses ensures the intended order of evaluation
190
+
echo 'Sum: ' . (1 + 2);
191
+
]]>
192
+
</programlisting>
193
+
<para>
194
+
If multiple arguments are passed in, then parentheses will not be
195
+
required to enforce precedence, because each expression is separate:
196
+
</para>
197
+
<programlisting role="php">
198
+
<![CDATA[
199
+
<?php
200
+
echo "Hello ", isset($name) ? $name : "John Doe", "!";
201
+

202
+
echo "Sum: ", 1 + 2;
203
+
]]>
204
+
</programlisting>
205
+
</tip>
139
206
</refsect1>
140
207

141
208
<refsect1 role="seealso">
...
...
@@ -145,7 +212,7 @@ echo $some_var ? 'true': 'false'; // changing the statement around
145
212
<member><function>print</function></member>
146
213
<member><function>printf</function></member>
147
214
<member><function>flush</function></member>
148
-
<member><link linkend="language.types.string.syntax.heredoc">Heredoc syntax</link></member>
215
+
<member><link linkend="language.types.string">Ways to specify literal strings</link></member>
149
216
</simplelist>
150
217
</para>
151
218
</refsect1>
152
219