reference/strings/functions/print.xml
7a3899eea90f3df8dcfe8fd350900162f3490bed
...
...
@@ -10,19 +10,20 @@
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
12
<type>int</type><methodname>print</methodname>
13
-
<methodparam><type>string</type><parameter>arg</parameter></methodparam>
13
+
<methodparam><type>string</type><parameter>expression</parameter></methodparam>
14
14
</methodsynopsis>
15
15
<para>
16
-
Outputs <parameter>arg</parameter>.
16
+
Outputs <parameter>expression</parameter>.
17
17
</para>
18
18
<para>
19
-
<literal>print</literal> is not actually a real function (it is a
20
-
language construct) so you are not required to use parentheses
21
-
with its argument list.
19
+
<literal>print</literal> is not a function but a language construct.
20
+
Its argument is the expression following the <literal>print</literal> keyword,
21
+
and is not delimited by parentheses.
22
22
</para>
23
23
<para>
24
-
The only difference to <literal>echo</literal> is that
25
-
<literal>print</literal> only accepts a single argument.
24
+
The major differences to <function>echo</function> are that
25
+
<literal>print</literal> only accepts a single argument and always returns
26
+
<literal>1</literal>.
26
27
</para>
27
28
</refsect1>
28
29

...
...
@@ -31,10 +32,12 @@
31
32
<para>
32
33
<variablelist>
33
34
<varlistentry>
34
-
<term><parameter>arg</parameter></term>
35
+
<term><parameter>expression</parameter></term>
35
36
<listitem>
36
37
<para>
37
-
The input data.
38
+
The expression to be output. Non-string values will be coerced to strings,
39
+
even when <link linkend="language.types.declarations.strict">the
40
+
<literal>strict_types</literal> directive</link> is enabled.
38
41
</para>
39
42
</listitem>
40
43
</varlistentry>
...
...
@@ -57,41 +60,36 @@
57
60
<programlisting role="php">
58
61
<![CDATA[
59
62
<?php
60
-
print("Hello World");
63
+
print "print does not require parentheses.";
61
64

62
-
print "print() also works without parentheses.";
65
+
// No newline or space is added; the below outputs "helloworld" all on one line
66
+
print "hello";
67
+
print "world";
63
68

64
-
print "This spans
69
+
print "This string spans
65
70
multiple lines. The newlines will be
66
71
output as well";
67
72

68
-
print "This spans\nmultiple lines. The newlines will be\noutput as well.";
73
+
print "This string spans\nmultiple lines. The newlines will be\noutput as well.";
69
74

70
-
print "escaping characters is done \"Like this\".";
75
+
// The argument can be any expression which produces a string
76
+
$foo = "example";
77
+
print "foo is $foo"; // foo is example
71
78

72
-
// You can use variables inside a print statement
73
-
$foo = "foobar";
74
-
$bar = "barbaz";
79
+
$fruits = ["lemon", "orange", "banana"];
80
+
print implode(" and ", $fruits); // lemon and orange and banana
75
81

76
-
print "foo is $foo"; // foo is foobar
82
+
// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
83
+
print 6 * 7; // 42
77
84

78
-
// You can also use arrays
79
-
$bar = array("value" => "foo");
85
+
// Because print has a return value, it can be used in expressions
86
+
// The following outputs "hello world"
87
+
if ( print "hello" ) {
88
+
echo " world";
89
+
}
80
90

81
-
print "this is {$bar['value']} !"; // this is foo !
82
-

83
-
// Using single quotes will print the variable name, not the value
84
-
print 'foo is $foo'; // foo is $foo
85
-

86
-
// If you are not using any other characters, you can just print variables
87
-
print $foo; // foobar
88
-

89
-
print <<<END
90
-
This uses the "here document" syntax to output
91
-
multiple lines with $variable interpolation. Note
92
-
that the here document terminator must appear on a
93
-
line with just a semicolon no extra whitespace!
94
-
END;
91
+
// The following outputs "true"
92
+
( 1 === 1 ) ? print 'true' : print 'false';
95
93
?>
96
94
]]>
97
95
</programlisting>
...
...
@@ -101,6 +99,85 @@ END;
101
99

102
100
<refsect1 role="notes">
103
101
&reftitle.notes;
102
+

103
+
<note>
104
+
<title>Using with parentheses</title>
105
+
<para>
106
+
Surrounding the argument to <literal>print</literal> with parentheses will not
107
+
raise a syntax error, and produces syntax which looks like a normal
108
+
function call. However, this can be misleading, because the parentheses are actually
109
+
part of the expression being output, not part of the <literal>print</literal>
110
+
syntax itself.
111
+
</para>
112
+
<para>
113
+
<example>
114
+
<title/>
115
+
<programlisting role="php">
116
+
<![CDATA[
117
+
<?php
118
+
print "hello";
119
+
// outputs "hello"
120
+

121
+
print("hello");
122
+
// also outputs "hello", because ("hello") is a valid expression
123
+

124
+
print(1 + 2) * 3;
125
+
// outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3
126
+
// the print statement sees the whole expression as one argument
127
+

128
+
if ( print("hello") && false ) {
129
+
print " - inside if";
130
+
}
131
+
else {
132
+
print " - inside else";
133
+
}
134
+
// outputs " - inside if"
135
+
// the expression ("hello") && false is first evaluated, giving false
136
+
// this is coerced to the empty string "" and printed
137
+
// the print construct then returns 1, so code in the if block is run
138
+
?>
139
+
]]>
140
+
</programlisting>
141
+
</example>
142
+
</para>
143
+

144
+
<para>
145
+
When using <literal>print</literal> in a larger expression, placing both the
146
+
keyword and its argument in parentheses may be necessary to give the intended
147
+
result:
148
+
</para>
149
+

150
+
<para>
151
+
<example>
152
+
<title/>
153
+
<programlisting role="php">
154
+
<![CDATA[
155
+
<?php
156
+
if ( (print "hello") && false ) {
157
+
print " - inside if";
158
+
}
159
+
else {
160
+
print " - inside else";
161
+
}
162
+
// outputs "hello - inside else"
163
+
// unlike the previous example, the expression (print "hello") is evaluated first
164
+
// after outputting "hello", print returns 1
165
+
// since 1 && false is false, code in the else block is run
166
+

167
+
print "hello " && print "world";
168
+
// outputs "world1"; print "world" is evaluated first,
169
+
// then the expression "hello " && 1 is passed to the left-hand print
170
+

171
+
(print "hello ") && (print "world");
172
+
// outputs "hello world"; the parentheses force the print expressions
173
+
// to be evaluated before the &&
174
+
?>
175
+
]]>
176
+
</programlisting>
177
+
</example>
178
+
</para>
179
+
</note>
180
+

104
181
&note.language-construct;
105
182
</refsect1>
106
183

...
...
@@ -111,7 +188,7 @@ END;
111
188
<member><function>echo</function></member>
112
189
<member><function>printf</function></member>
113
190
<member><function>flush</function></member>
114
-
<member><link linkend="language.types.string.syntax.heredoc">Heredoc syntax</link></member>
191
+
<member><link linkend="language.types.string">Ways to specify literal strings</link></member>
115
192
</simplelist>
116
193
</para>
117
194
</refsect1>
118
195