diff --git a/language/control-structures.xml b/language/control-structures.xml
index 3ef4d1b7f3..8adff8a756 100644
--- a/language/control-structures.xml
+++ b/language/control-structures.xml
@@ -28,6 +28,7 @@
arrayechoeval
+ print
diff --git a/reference/strings/functions/echo.xml b/reference/strings/functions/echo.xml
index 3f136d3951..12c3479026 100644
--- a/reference/strings/functions/echo.xml
+++ b/reference/strings/functions/echo.xml
@@ -10,27 +10,23 @@
&reftitle.description;
voidecho
- stringarg
- stringargs
+ stringexpressions
- Outputs all parameters. No additional newline is appended.
+ Outputs one or more expressions, with no additional newlines or spaces.
- echo is not actually a function (it is a
- language construct), so you are not required to use parentheses
- with it. echo (unlike some other language
- constructs) does not behave like a function, so it cannot
- always be used in the context of a function. Additionally, if you want to
- pass more than one parameter to echo, the parameters
- must not be enclosed within parentheses.
+ echo is not a function but a language language construct.
+ Its arguments are a list of expressions following the echo
+ keyword, separated by commas, and not delimited by parentheses.
+ Unlike some other language constructs, echo does not have
+ any return value, so it cannot be used in the context of an expression.
echo also has a shortcut syntax, where you can
- immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0,
- this short syntax only works with the
- short_open_tag configuration
- setting enabled.
+ immediately follow the opening tag with an equals sign. This syntax is available
+ even with the short_open_tag configuration
+ setting disabled.
foo.
- The major differences to print are that
- echo accepts an argument list and doesn't have a return value.
+ The major differences to print are that
+ echo accepts multiple arguments and doesn't have a return value.
@@ -50,17 +46,13 @@ I have =$foo?> foo.
- arg
-
-
- The parameter to output.
-
-
-
-
- args
+ expressions
+ One or more string expressions to output, separated by commas.
+ Non-string values will be coerced to strings, even when
+ the
+ strict_types directive is enabled.
@@ -83,22 +75,45 @@ I have =$foo?> foo.
]]>
@@ -109,31 +124,82 @@ echo $some_var ? 'true': 'false'; // changing the statement around
&reftitle.notes;
¬e.language-construct;
+
+
+ Using with parentheses
+
+ Surrounding a single argument to echo with parentheses will not
+ raise a syntax error, and produces syntax which looks like a normal
+ function call. However, this can be misleading, because the parentheses are actually
+ part of the expression being output, not part of the echo
+ syntax itself.
+
+
+
+
+
+
+]]>
+
+
+
+
+
- A benefit to passing in multiple arguments over using concatenation in
- echo regards the precedence of the period operator in
- PHP. If multiple arguments are passed in, then parentheses will not be
- required to enforce precedence:
+ Passing multiple arguments to echo can avoid
+ complications arising from the precedence of the concatenation operator in
+ PHP. For instance, the concatenation operator has higher precedence than
+ the ternary operator, and prior to PHP 8.0.0 had the same precedence as addition
+ and subtraction:
-
-
- With concatenation, the period operator has the same precedence as
- the addition operator, and higher precedence than the ternary operator, so parentheses must be used for the
- correct behaviour:
+ If multiple arguments are passed in, then parentheses will not be
+ required to enforce precedence, because each expression is separate:
@@ -146,7 +212,7 @@ echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';
printprintfflush
- Heredoc syntax
+ Ways to specify literal strings
diff --git a/reference/strings/functions/print.xml b/reference/strings/functions/print.xml
index 8247cfc8b0..6512037391 100644
--- a/reference/strings/functions/print.xml
+++ b/reference/strings/functions/print.xml
@@ -10,19 +10,20 @@
&reftitle.description;
intprint
- stringarg
+ stringexpression
- Outputs arg.
+ Outputs expression.
- print is not actually a real function (it is a
- language construct) so you are not required to use parentheses
- with its argument list.
+ print is not a function but a language language construct.
+ Its argument is the expression following the print keyword,
+ and is not delimited by parentheses.
- The major differences to echo are that
- print only accepts a single argument and always returns 1.
+ The major differences to echo are that
+ print only accepts a single argument and always returns
+ 1.
@@ -31,10 +32,12 @@
- arg
+ expression
- The input data.
+ The expression to be output. Non-string values will be coerced to strings,
+ even when the
+ strict_types directive is enabled.
@@ -57,41 +60,36 @@
"foo");
+// Because print has a return value, it can be used in expressions
+// The following outputs "hello world"
+if ( print "hello" ) {
+ echo " world";
+}
-print "this is {$bar['value']} !"; // this is foo !
-
-// Using single quotes will print the variable name, not the value
-print 'foo is $foo'; // foo is $foo
-
-// If you are not using any other characters, you can just print variables
-print $foo; // foobar
-
-print <<
]]>
@@ -101,6 +99,85 @@ END;
&reftitle.notes;
+
+
+ Using with parentheses
+
+ Surrounding the argument to print with parentheses will not
+ raise a syntax error, and produces syntax which looks like a normal
+ function call. However, this can be misleading, because the parentheses are actually
+ part of the expression being output, not part of the print
+ syntax itself.
+
+
+
+
+
+
+]]>
+
+
+
+
+
+ When using print in a larger expression, placing both the
+ keyword and its argument in parentheses may be necessary to give the intended
+ result:
+
+
+
+
+
+
+
+]]>
+
+
+
+
+
¬e.language-construct;
@@ -111,7 +188,7 @@ END;
echoprintfflush
- Heredoc syntax
+ Ways to specify literal strings