php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42644 For loop comma documentation wrong
Submitted: 2007-09-12 15:43 UTC Modified: 2007-11-21 16:59 UTC
From: steve dot seear at uk dot ibm dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: steve dot seear at uk dot ibm dot com
New email:
PHP Version: OS:

 

 [2007-09-12 15:43 UTC] steve dot seear at uk dot ibm dot com
Description:
------------
The for loop documentation page at  http://uk3.php.net/manual/en/control-structures.for.php states that "Comma separated expressions in expr2 are treated similarly to being separated by the || operator."

The supplied code shows that this is not the case. According to the documentation, all the loops in the code fragment should iterate twice, whereas actually, loop 2 only iterates once. For comma separated conditions, it appears that only the last condition in the comma separated list is tested.

Reproduce code:
---------------
<?php
// comma operator in condition
for ($a = $b = 0; $a < 1, $b < 2; $a++, $b++) {
	echo "loop 1, iteration $a\n";
}
echo "\n";
for ($a = $b = 0; $b < 2, $a < 1; $a++, $b++) {
	echo "loop 2, iteration $a\n";
}
echo "\n";
// || operator in condition
for ($a = $b = 0; $a < 1 || $b < 2; $a++, $b++) {
	echo "loop 3, iteration $a\n";
}
echo "\n";
for ($a = $b = 0; $b < 2 || $a < 1; $a++, $b++) {
	echo "loop 4, iteration $a\n";
}
echo "\n";
?>

Expected result:
----------------
loop 1, iteration 0
loop 1, iteration 1

loop 2, iteration 0
loop 2, iteration 1

loop 3, iteration 0
loop 3, iteration 1

loop 4, iteration 0
loop 4, iteration 1

Actual result:
--------------
loop 1, iteration 0
loop 1, iteration 1

loop 2, iteration 0

loop 3, iteration 0
loop 3, iteration 1

loop 4, iteration 0
loop 4, iteration 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-13 11:09 UTC] rquadling@php.net
<?php
$c = 0;
for ($i = 0 ; true, $c++, false ; ++$i) {
	echo 'Loop 1 ', $i, PHP_EOL;
	if (2 == $i) {
		echo '***Expression 2 failed***', PHP_EOL;
		break;
	}
}
echo $c, PHP_EOL;

for ($i = 0 ; false, $c++, true ; ++$i) {
	echo 'Loop 2 ', $i, PHP_EOL;
	if (2 == $i) {
		echo '***Expression 2 failed***', PHP_EOL;
		break;
	}
}
echo $c, PHP_EOL;
?>

outputs ...

1
Loop 2 0
Loop 2 1
Loop 2 2
***Expression 2 failed***
4


This shows that expr2 is processed in it entirety but only uses the final element to determine if the loop should continue.

Tested with :
PHP 4.4.7-dev (cgi-fcgi) (built: Mar 25 2007 21:02:43)
PHP 5.2.5-dev (cli) (built: Sep 13 2007 04:04:33)
PHP 6.0.0-dev (cli) (built: Sep 13 2007 02:04:41)


 [2007-11-21 16:59 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part."
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 08:00:01 2026 UTC