php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #74362 Make echo and print equivalent
Submitted: 2017-04-02 12:42 UTC Modified: 2017-04-23 10:10 UTC
From: olafvdspek at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: olafvdspek at gmail dot com
New email:
PHP Version: OS:

 

 [2017-04-02 12:42 UTC] olafvdspek at gmail dot com
Description:
------------
Could print be made equivalent to echo such that it accepts multiple arguments?

> The only difference to print is that echo accepts an argument list.
> The only difference to echo is that print only accepts a single argument.

http://php.net/echo
http://php.net/print

Test script:
---------------
<?php
echo('A', 2, 'C');
print('A', 2, 'C');

Actual result:
--------------
-

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-04-02 12:45 UTC] kelunik@php.net
That's not the only difference, the return types also vary. "echo" is "void" while "print" returns "int".
 [2017-04-02 13:03 UTC] dm@php.net
`print` has a return value along with outputting and can be used as e.g. `return print json_encode(...);` while `echo` can not.
 [2017-04-02 13:18 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: PHP Language Specification +Package: Scripting Engine problem
 [2017-04-02 13:22 UTC] olafvdspek at gmail dot com
> That's not the only difference,

OK, so the documentation is wrong too?

@dm: sure, but that doesn't mean print couldn't support multiple arguments does it?
 [2017-04-02 15:33 UTC] bwoebi@php.net
Small example:

foo(print 1, 2);
^ is 2 a second argument to print or to foo?

That's ambiguous, hence we do not allow that.
 [2017-04-03 03:52 UTC] pollita@php.net
True about the difference being slightly larger, but as far as the original question goes: Yes, we could allow print to accept multiple arguments.

For that matter we could make echo return a value.  Neither is going to break anything[citation needed], and due to the way the compiler handles both statements, neither will perform worse (fun fact: the compiler turn both into ZEND_ECHO opcodes, just with a different extended_value which it doesn't even use).

Should we? It'd be a grammar change, so you'll need an RFC and a 2/3 vote in favor of it.  Without a compelling reason, I doubt you'll get that.
 [2017-04-03 03:55 UTC] pollita@php.net
Oh, and because I missed bwoebi@'s comment...  That's exactly the kind of edge case I included "[citation needed]" in my reply for. :)

Taht said, we could resolve that ambiguity by requiring parenthesis for print with multiple args, but at that point you have an exeption to the grammar rule, and you're not longer bringing echo/orint in line with each other and any consistency benefit flies away.
 [2017-04-03 19:50 UTC] olafvdspek at gmail dot com
1. Could the docs be fixed?
2. Is the return value of print of any use?
3. I'd love the multiple arguments with parentheses variant to work but I guess that's a bridge too bar.
 [2017-04-05 14:50 UTC] fractalesque at gmail dot com
Ignoring the other points, the documentation have been fixed.
 [2017-04-05 15:37 UTC] olafvdspek at gmail dot com
Thx
The example syntax ("void echo ( string $arg1 [, string $... ] ) is still using parenthesis though it doesn't work with multiple arguments and nikic says it's not the 'correct' syntax.

https://bugs.php.net/bug.php?id=74363
 [2017-04-23 10:10 UTC] olafvdspek at gmail dot com
Knock knock ;)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 20:01:29 2024 UTC