php.net |  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
28 - 20 = ?
Subscribe to this entry?

 
 [2018-06-05 10:30 UTC] aleemb at gmail dot com
Description:
------------
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:
----------------
foo

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-05 10:32 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-06-05 10:32 UTC] requinix@php.net
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] requinix@php.net
> 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] yohgaki@php.net
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
http://php.net/manual/en/function.echo.php

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 22:01:29 2024 UTC