#45410 [Bgs]: Bad result in an expression evaluation
ID: 45410
User updated by: ensnnet at gmail dot com
Reported By: ensnnet at gmail dot com
Status: Bogus
Bug Type: Unknown/Other Function
Operating System: windows xp sp3
PHP Version: 5.2.6
New Comment:
Thank for your answer johannes@php.net but still I think someone is not
correct, perhaps I am wrong.
I have read your link:
http://es2.php.net/manual/en/language.operators.logical.php:
And y read there:
$a and $b => TRUE if both $a and $b are TRUE.
I had writed another simple code that dont run as expected (I send you
at the end of this note). The result that show on my computer is:
$l1 = TRUE, $l2 = FALSE => $l1 and $l2 is FALSE
$l1 = TRUE, $l2 = FALSE => $l1 and $l2 is TRUE
$l1 = TRUE, $l2 = FALSE => $l1 and $l2 is FALSE
$l1 = TRUE, $l2 = FALSE => $l1 and $l2 is FALSE
I think the second line must be as the another lines. What is wrong?
Thanks for your attention,
jaz
This is the code:
<?php
$v1 = "A";
$v2 = 0;
$l1 = $v1=="A";
$l2 = $v2!=0;
$flag1 = $l1 and $l2;
$flag2 = ($l1 and $l2);
$flag3 = $l2 and $l1;
echo "\$l1 = ".showAstxt($l1).", \$l2 = ".showAstxt($l2)." => \$l1 and
\$l2 is ".showAstxt($l1 and $l2)."<br>";
echo "\$l1 = ".showAstxt($l1).", \$l2 = ".showAstxt($l2)." => \$l1 and
\$l2 is ".showAstxt($flag1)."<br>";
echo "\$l1 = ".showAstxt($l1).", \$l2 = ".showAstxt($l2)." => \$l1 and
\$l2 is ".showAstxt($flag2)."<br>";
echo "\$l1 = ".showAstxt($l1).", \$l2 = ".showAstxt($l2)." => \$l1 and
\$l2 is ".showAstxt($flag3)."<br>";
// ============================================================
function showAstxt($v)
{
return $v ? "TRUE" : "FALSE";
}
?>
Previous Comments:
------------------------------------------------------------------------
[2008-07-01 23:08:37] johannes@php.net
sorry wrong URL, correct one: http://php.net/operators
------------------------------------------------------------------------
[2008-07-01 23:07:14] johannes@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
The assignment is executed before the "and" operator, see the
precedence table in the docs, http://php.net/operator
------------------------------------------------------------------------
[2008-07-01 21:33:57] ensnnet at gmail dot com
Description:
------------
A simple logical expression is bad evaluated. Next code show that.
Reproduce code:
---------------
<?php
$emma = "A";
$lobera = 0;
echo "[ The right answer is: Flag is off ]<br><br>";
// This run bad:
$flag = $emma=="A" and $lobera!=0;
evaluate($flag);
// This run well (why?):
$flag = ($emma=="A" and $lobera!=0);
evaluate($flag);
// This run well too:
$flag = $lobera!=0 and $emma=="A";
evaluate($flag);
// ============================================================
function evaluate($flag)
{
if($flag)
echo "Flag is on<br>";
else
echo "Flag is off<br>";
}
?>
Expected result:
----------------
[ The right answer is: Flag is off ]
Flag is off
Flag is off
Flag is off
Actual result:
--------------
[ The right answer is: Flag is off ]
Flag is on <--- This is bad
Flag is off
Flag is off
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45410&edit=1
Thread (10 messages)