php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45410 Bad result in an expression evaluation
Submitted: 2008-07-01 21:33 UTC Modified: 2008-07-14 14:41 UTC
From: ensnnet at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.6 OS: windows xp sp3
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ensnnet at gmail dot com
New email:
PHP Version: OS:

 

 [2008-07-01 21:33 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-01 23:07 UTC] 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 23:08 UTC] johannes@php.net
sorry wrong URL, correct one: http://php.net/operators
 [2008-07-02 17:12 UTC] ensnnet at gmail dot com
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";
}
?>
 [2008-07-02 19:03 UTC] johannes@php.net
Please see the operator precedence table and explanation on the page I ahve given, somewhere in the upper area are "&&", "||" and so on, then "=" and in the bottom "and", "or", "xor" which means the assignment is done before the "and" operator is executed. For further questions please refer to other support forums, see http://php.net/support.php
 [2008-07-02 21:32 UTC] colder@php.net
Please take a look at the examples on php.net/operators.logical
 [2008-07-11 17:55 UTC] ensnnet at gmail dot com
Sorry by I still I think something is wrong.  Please take a while to see the next code (or run it on your machine):

<?php
$l1		= true;
$l2		= false;
$l1_and_l2	= $l1 and $l2;
$l2_and_l1	= $l2 and $l1;

echo "<pre>";
echo "Here l1 and l2 is: ";
var_dump($l1 and $l2);

echo "But here l1 and l2 is: ";
var_dump($l1_and_l2);

echo "And here l2 and l1 is: ";
var_dump($l2_and_l1);

echo "What happens?";
echo "</pre>";
?>
 [2008-07-11 17:59 UTC] ensnnet at gmail dot com
Te answer on my machine for the previous code is:

Here l1 and l2 is: bool(false)
But here l1 and l2 is: bool(true)
And here l2 and l1 is: bool(false)
What happens?

... And I think it must be false in all the three cases !!

Thanks in advance for your attention,
jaz
 [2008-07-14 11:22 UTC] mgf@php.net
*Please* look at Example 1 at http://php.net/operators.logical, as requested by colder - this demonstrates your example exactly and shows that what you're getting is the expected result.  The operator precedence table at http://php.net/operators#language.operators.precedence (also previously referred to) may also help.

This is *not* a bug, so don't add to this conversation.  If you need further explanation, please ask in the support forums (such as php-general@lists.php.net).
 [2008-07-14 14:41 UTC] ensnnet at gmail dot com
Thanks, I was wrong (sorry for my insistence).  Assignement (=) is done before the and/or opperation but the &&/|| opperation is done before the assignement (=).

Thanks very much for your attention and explanation.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 20 21:00:02 2025 UTC