#24626 [Opn->Bgs]: foreach stops with boolen expresion in loop
| From: | elmicha@php.net | Date: | Sat, 12 Jul 2003 23:23:50 +0000 |
| Subject: | #24626 [Opn->Bgs]: foreach stops with boolen expresion in loop | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-43830@lists.php.net to get a copy of this message | ||
ID: 24626
Updated by: elmicha@php.net
Reported By: eric at austarmetro dot com dot au
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Win XP
PHP Version: 4.3.3RC1
New Comment:
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.
Thank you for your interest in PHP.
"&&" is a boolean operator, "&" works bitwise (on integers, not on
strings). "&=" also works bitwise.
And $c == 'two' does never happen in the script you posted. If you
remove the spaces from your $a values, $status('two') will return 0,
which makes $status 0. After that, the status() function is never
called again, because $status already is 0, which is equivalent to
false ("short-circuit evaluation"). Stick some echos in the loop and
you'll see that it works just fine.
Previous Comments:
------------------------------------------------------------------------
[2003-07-12 17:45:22] eric at austarmetro dot com dot au
Description:
------------
The foreach loop exits early and sets the $status variable to "".
The problem is fixed by changing
$status = $status && status( $b );
to
$status &= status( $b );
Hope this is bogus :)
I have tried this on WinXP PHP4.3.3RC1 and FREEBSD PHP4.3.1 both give
the same result.
Eric
Reproduce code:
---------------
<?
function status( $c ) {
echo $c;
if( $c == 'two' )
return 0;
return 1;
}
$a = array( 'one ','two ','three ','four ' );
$status = 1;
foreach( $a as $b )
{
$status = $status && status( $b );
}
echo " foreach stopped. Status = ".$status;
if( isset( $status ) )
echo " set ";
else
echo "unset";
?>
Expected result:
----------------
onetwothreefour foreach stopped. Status = 0 set
Actual result:
--------------
onetwo foreach stopped. Status = set
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=24626&edit=1