#42644 [Opn->Ver]: For loop comma documentation wrong
| From: | rquadling@php.net | Date: | Thu, 13 Sep 2007 11:09:59 +0000 |
| Subject: | #42644 [Opn->Ver]: For loop comma documentation wrong | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8@lists.php.net to get a copy of this message | ||
ID: 42644
Updated by: rquadling@php.net
Reported By: steve dot seear at uk dot ibm dot com
-Status: Open
+Status: Verified
Bug Type: Documentation problem
Operating System: Windows
PHP Version: Irrelevant
New Comment:
<?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)
Previous Comments:
------------------------------------------------------------------------
[2007-09-12 15:43:43] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42644&edit=1