Bug #75114 [Opn->Nab]: anomaly when assigning a var in an if-statement

From: Date: Thu, 24 Aug 2017 17:38:28 +0000
Subject: Bug #75114 [Opn->Nab]: anomaly when assigning a var in an if-statement
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210797@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75114&edit=1 ID: 75114 Updated by: kalle@php.net Reported by: php at hitchhikers11 dot com Summary: anomaly when assigning a var in an if-statement -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#210797) next »