#45410 [Bgs]: Bad result in an expression evaluation

From: Date: Tue, 01 Jul 2008 23:08:37 +0000
Subject: #45410 [Bgs]: Bad result in an expression evaluation
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-126169@lists.php.net to get a copy of this message
 ID:               45410
 Updated by:       johannes@php.net
 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:

sorry wrong URL, correct one: http://php.net/operators


Previous Comments:
------------------------------------------------------------------------

[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)

« previous php.bugs (#126169) next »