Edit report at https://bugs.php.net/bug.php?id=75114&edit=1
ID: 75114
User updated by: php at hitchhikers11 dot com
Reported by: php at hitchhikers11 dot com
Summary: anomaly when assigning a var in an if-statement
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Got it, thanks!
Previous Comments:
------------------------------------------------------------------------
[2017-08-24 17:38:26] kalle@php.net
This is because the if conditional is evaluated as:
$var = "value" || false;
meaning that $var is now a boolean, as (bool)"value" === true, therefore causing
"1" to be printed when echoing the string.
What you need to do is to set the priority of evaluation like:
($var = 'value') || false
This will yield your desired behavior
------------------------------------------------------------------------
[2017-08-24 13:00:06] php at hitchhikers11 dot com
Description:
------------
There is a strange behaveour,
when assigning a value in an if-statement (=),
instead of equate the variable with a value (==):
the value of the variable becomes TRUE respectively 1,
this happens only if there is another condition in the
if-statement after assigning the variable.
Tested with PHP Version 5.6 and 7.?
Test script:
---------------
$var = "original value";
$var2 = 'some_value';
echo "1: ".$var."<br>";
if($var="value" || false){ echo "2: ".$var."<br>"; } /*
&& or || */
if("any_true_value"==$var){echo "3: ".$var."<br>";}
Expected result:
----------------
1: original value
2: value
Actual result:
--------------
1: original value
2: 1
3: 1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75114&edit=1