php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44934 The parser doesn't parse a comma
Submitted: 2008-05-07 11:52 UTC Modified: 2013-02-17 12:34 UTC
From: php at hristov dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.3CVS-2008-05-07 (CVS) OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-05-07 11:52 UTC] php at hristov dot com
Description:
------------
In the following case the parser bails out:

php -r 'echo true? $t++,"true":"false";'

with

Parse error: syntax error, unexpected ',' in Command line code on line 1

The following mini-C program however does compile, although gcc is kind of stupid to realize that there should be no warning:

#include <stdio.h>
int main(void)
{
  int t=0, f=0;
  printf("%s\n", (1==1) ? t++,"True":"False");
}
andrey@whirlpool:~/dev> gcc -o a a.c
andrey@whirlpool:~/dev> ./a
True

The comma does work however when used in a for() loop :
andrey@whirlpool:~/dev> php -r 'for($i=0; $a++, $i < 2; $i++) echo "-"; echo "\n";'
--


Reproduce code:
---------------
php -r 'echo true? $t++,"true":"false";'

Expected result:
----------------
true


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-07 14:26 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

The ternary operator is not supposed to support that syntax, and it doesn't.
So this is not a bug, and I'm not sure it's a good feature request either: ternary syntax is suited for simple cases. But if you really think it should be implemented, file a feature request.
 [2008-05-07 14:47 UTC] andrey@php.net
If introduced while not completely?
 [2008-05-07 14:55 UTC] andrey@php.net
To make it even easier for testing :
./php -r '$a=1; $b = ($a, 2);'

The equivalent in C is:
#include <stdio.h>
int main(void)
{
  int a = 1, c;
  c = (a, 2);
  printf("%d\n", c);
}

"," has lower prio than =, thus the brackets.
 [2008-05-07 15:06 UTC] jani@php.net
What exactly is supposed to happen with that php code above? 
I mean, whatta hell is $b = ($a, 1); supposed to do?
 [2008-05-07 15:07 UTC] jani@php.net
Other than cause a parse error of course..
 [2008-05-07 15:27 UTC] php at hristov dot com
Give you "2" as result.
This is just an example, to simplify what is wanted, not something you will find somewhere.
 [2008-05-07 15:47 UTC] tony2001@php.net
Oh, please no need to add THAT kind of unreadable syntax, we have enough of it already.
This one is really ugly and more important - useless.
 [2013-02-17 12:34 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2013-02-17 12:34 UTC] nikic@php.net
No need for the comma operator in PHP, only serves to confuse people. Marking as Wfx.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 17:01:33 2024 UTC