|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76414 return echo("foo"); ought to work as expected
Submitted: 2018-06-05 10:30 UTC Modified: 2018-06-05 11:36 UTC
From: aleemb at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.2.6 OS: OSX
Private report: No CVE-ID: None
 [2018-06-05 10:30 UTC] aleemb at gmail dot com
This is either a bug or a feature request, either way the following should work as expected:

    return echo("foo");

It should echo "foo" and return.

Test script:
return echo("foo");

Expected result:

Actual result:
PHP Parse error:  syntax error, unexpected 'echo' (T_ECHO), expecting ';'


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-05 10:32 UTC]
-Status: Open +Status: Not a bug
 [2018-06-05 10:32 UTC]
Use print().
 [2018-06-05 11:16 UTC] aleemb at gmail dot com
@requinix, is there any reason echo can't be tweaked to behave in the same manner?
 [2018-06-05 11:20 UTC] spam2 at rhsoft dot net
echo is *not* a function - it's a language costruct
echo $a, "\n", $b, "\n";

this would not be possible with a function and the difference is that is saves a ton of concat calls compared to echo $a.  "\n" . $b . "\n" which does exactly the same but becaus eof the repeatet concat a ton of in-memory-copies a triggered which can be expensive depending on the string sizes
 [2018-06-05 11:31 UTC]
> is there any reason echo can't be tweaked to behave in the same manner?
Because there's no reason to? echo and print are both doing exactly what they're intended to do.
 [2018-06-05 11:36 UTC]
Since echo is not a function, you cannot do

$var ? echo 'abc' : echo 'xyz';

also. It's not a function, so echo wouldn't return absolutely nothing. Therefore, it cannot be used anywhere expression is expected.
 [2018-06-05 11:36 UTC] aleemb at gmail dot com
I was assuming since neither are language constructs, they could share the same "return" semantics. Your explanation, however, helps clarify that. Thanks for taking the time to highlight it, learnt something new.
 [2018-06-05 11:53 UTC] spam2 at rhsoft dot net

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.
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Mar 23 18:01:26 2019 UTC