php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42140 comma expression doesn't work outside for operator
Submitted: 2007-07-29 14:31 UTC Modified: 2007-07-31 20:49 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: dmitry at rsl dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.3 OS: linux
Private report: No CVE-ID: None
 [2007-07-29 14:31 UTC] dmitry at rsl dot ru
Description:
------------
comma expression doesn't work outside for operator:
this works:

<? for($z = $a=2,$b=1;false;); echo "z=$z a=$a b=$b"; ?>

this doesn't:
<? $z = $a=2,$b=1; echo "z=$z a=$a b=$b"; ?>
Parse error: syntax error, unexpected ',' in /home/dmitry/x.php on line 1

Reproduce code:
---------------
<? $z = $a=2,$b=1; echo "z=$z a=$a b=$b"; ?>

Expected result:
----------------
z=2 a=2 b=1

Actual result:
--------------
Parse error: syntax error, unexpected ',' in /home/dmitry/x.php on line 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-29 15:26 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This syntax is simply not supported, and that is expected.
 [2007-07-29 16:06 UTC] dmitry at rsl dot ru
Actually documentation has nothing about comma expression, the only way to know about it is to view example 4 of 'for' operator. Though, it is also mentioned but not explained in Operator Precedence part of chapter 15.

Well, this syntax works inside 'for' structure, so it is supported, why not to allow to use this syntax in other expressions also?
 [2007-07-29 23:37 UTC] jani@php.net
Because it doesn't make any sense elsewhere.
(Disclaimer: I can not think of any other places where it would be useful, your example is not the best one..)
 [2007-07-30 08:09 UTC] dmitry at rsl dot ru
How about following examples:

while ($a = mysql_fetch_array(),$rec_no++) {
}

$to_pay = ($left<$total)?($left,$stop=1):$total;
 [2007-07-31 11:46 UTC] jani@php.net
I find that very confusing and stupid syntax. Just put the stuff inside the loop like in all other languages. Don't reopen this anymore, this is no bug.
 [2007-07-31 20:49 UTC] dmitry at rsl dot ru
Other languages? Let's see:

[dmitry@srv dmitry]$ cat x.c; gcc x.c; ./a.out
#include<stdio.h>
int a,b,c;
main()
{
        c = a=1,b=2;
        printf("a=%d b=%d c=%d\n",a,b,c);
}
a=1 b=2 c=1

[dmitry@srv dmitry]$ cat x.pl;./x.pl
#!/usr/bin/perl
$c = $a=1, $b=2;

print "a=$a b=$b c=$c\n";
a=1 b=2 c=1

Guess the result of following:
<script>
c = a=1,b=2;
alert('a='+a+' b='+b+' c='+c);
</script>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 11:01:36 2025 UTC