php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66772 Comma operator does not behave as operator
Submitted: 2014-02-25 20:35 UTC Modified: 2016-03-31 16:12 UTC
From: cmbecker69 at gmx dot de Assigned: cmb (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
 [2014-02-25 20:35 UTC] cmbecker69 at gmx dot de
Description:
------------
The section "Operator Precedence"[1] lists all operators and gives
a link to the manual section where they are described; for the
comma operator it only says "many uses". It seems to be
appropriate to list these many uses in an own section.

OTOH, from what I can tell from Zend/zend_language_parser.y[2] the
only actual place where the comma is used as operator (opposed to
a list delimiter) is inside for_expr. This usage is explained in
the "for" section[3]. If this is indeed the only use, the operator
precendence table seems to be in error.

[1] <http://www.php.net/manual/en/language.operators.precedence.php>
[2] <http://lxr.php.net/xref/PHP_5_5/Zend/zend_language_parser.y>
[3] <http://www.php.net/manual/en/control-structures.for.php>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-06 02:52 UTC] yohgaki@php.net
I think ',' is used to divide expressions just like C/C++, isn't? Therefore, 

echo 1,2,3,4; 

as well as

$i=0;
echo ++$i, ++$i, ++$i;

works.

http://en.cppreference.com/w/cpp/language/operator_precedence
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

I'm not sure what do you mean by "the operator precedence table seems to be in error."
 [2014-07-06 10:52 UTC] cmbecker69 at gmx dot de
> I think ',' is used to divide expressions just like C/C++,
> isn't?

It doesn't seem to be in the general case.  For instance:

  $a = (1, 2); // => syntax error, unexpected ','
  
Furthermore in C the comma operator doesn't simply divide
expressions, but is defined to evaluate the first operand
discarding the result, and than to evaluate the second operand
"returning" its result.
  
> echo 1,2,3,4;

This is possible due to the special grammar rule echo_expr_list.

> I'm not sure what do you mean by "the operator precedence table
> seems to be in error."

The *only* case where the comma can be used as *operator* seems to
be inside for_expr. However, the documentation states the comma
operator has *many* uses.
 [2014-07-07 05:38 UTC] yohgaki@php.net
-Summary: Comma operator has insufficient documentation +Summary: Comma operator does not behave as operator -Type: Documentation Problem +Type: Bug -Package: Documentation problem +Package: Scripting Engine problem
 [2014-07-07 05:38 UTC] yohgaki@php.net
Reasonable discussion.

Since this is documented for a long time, it may be good to categorize as engine issue.
 [2014-07-07 11:44 UTC] cmbecker69 at gmx dot de
> Since this is documented for a long time, it may be good to
> categorize as engine issue.

Please note, that I do not suggest to implement a universal comma
operator as in C/C++. Actually, I believe that it is not necessary
and doesn't have real benefits (at least in PHP).

IMHO it would be sufficient to document the comma operator more
clearly, or perhaps, to not categorize it as *operator* at all
(Java does not have a comma operator[1], but similar uses of the
comma as in PHP).

[1]
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.27-120>
 [2014-07-07 13:44 UTC] nikic@php.net
-Type: Bug +Type: Documentation Problem -Package: Scripting Engine problem +Package: Documentation problem
 [2014-07-07 13:44 UTC] nikic@php.net
@yasuo: This is docs problem. PHP has no comma operator, it only uses commas as an expression delimiter in some places, like function arguments.
 [2016-03-31 16:11 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=338860
Log: fixed #66772 (Comma operator does not behave as operator)
 [2016-03-31 16:12 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=a8b24da312bc04d20f404f99c90d2705e850ec16
Log: fixed #66772 (Comma operator does not behave as operator)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 20:01:29 2024 UTC