note 120882 added to function.echo

From: Date: Tue, 28 Mar 2017 02:51:57 +0000
Subject: note 120882 added to function.echo
Groups: php.notes 
Request: Send a blank email to php-notes+get-208957@lists.php.net to get a copy of this message
Passing multiple parameters to echo using commas (',')is not exactly identical to using the concatenation operator ('.'). There are two notable differences. First, concatenation operators have much higher precedence. Referring to http://php.net/operators.precedence, there are many operators with lower precedence than concatenation, so it is a good idea to use the multi-argument form instead of passing concatenated strings. <?php echo "The sum is " . 1 | 2; // output: "2". Parentheses needed. echo "The sum is ", 1 | 2; // output: "The sum is 3". Fine. ?> Second, a slightly confusing phenomenon is that unlike passing arguments to functions, the values are evaluated one by one. <?php function f($arg){ var_dump($arg); return $arg; } echo "Foo" . f("bar") . "Foo"; echo "\n\n"; echo "Foo", f("bar"), "Foo"; ?> The output would be: string(3) "bar"FoobarFoo Foostring(3) "bar" barFoo It would become a confusing bug for a script that uses blocking functions like sleep() as parameters: <?php while(true){ echo "Loop start!\n", sleep(1); } ?> vs <?php while(true){ echo "Loop started!\n" . sleep(1); } ?> With ',' the cursor stops at the beginning every newline, while with '.' the cursor stops after the 0 in the beginning every line (because sleep() returns 0). ---- Server IP: 72.52.91.14 Probable Submitter: 14.199.241.195 ---- Manual Page -- http://php.net/manual/en/function.echo.php Edit -- https://master.php.net/note/edit/120882 Del: integrated -- https://master.php.net/note/delete/120882/integrated Del: useless -- https://master.php.net/note/delete/120882/useless Del: bad code -- https://master.php.net/note/delete/120882/bad+code Del: spam -- https://master.php.net/note/delete/120882/spam Del: non-english -- https://master.php.net/note/delete/120882/non-english Del: in docs -- https://master.php.net/note/delete/120882/in+docs Del: other reasons-- https://master.php.net/note/delete/120882 Reject -- https://master.php.net/note/reject/120882 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208957) next »