Bug #74871 [Com]: if statement behavior
| From: | spam2 at rhsoft dot net | Date: | Fri, 07 Jul 2017 08:52:10 +0000 |
| Subject: | Bug #74871 [Com]: if statement behavior | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209884@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74871&edit=1
ID: 74871
Comment by: spam2 at rhsoft dot net
Reported by: johovich at yande dot ru
Summary: if statement behavior
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Linux version 2.6.32-531.23.3.lv
PHP Version: 7.1.7
Block user comment: N
Private report: N
New Comment:
and "if($pos = 0)" is always true because = assigns a value while == and === compare a
value
[harry@srv-rhsoft:/downloads]$ php test.php
DO NOT USE A SINGLE = TO COMPARE SOMETHING
[harry@srv-rhsoft:/downloads]$ cat test.php
<?php
if($test = 1)
{
echo "DO NOT USE A SINGLE = TO COMPARE SOMETHING\n";
}
?>
Previous Comments:
------------------------------------------------------------------------
[2017-07-07 00:04:56] rtheunissen@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
This is expected behaviour, an if statement defaults to a non-strict boolean comparison.
------------------------------------------------------------------------
[2017-07-07 00:01:30] rtheunissen@php.net
This is not a bug.
if ($condition) is the same as if ($condition == true),
so in the second part of your test script if you changed the strict !== to
!=, you would get the same results.
So you're effectively saying if (($pos = 0) == true) which will drop to the
else.
------------------------------------------------------------------------
[2017-07-06 23:51:22] johovich at yande dot ru
Description:
------------
If i use set var inside 'if' statement condition $var = '0' is the same as $var
= false. But it is not the same. So if assign var to function that returns 0 as integer or 0 as
string 'if' statement condition works as false.
Test script:
---------------
//In this script you can see, that set var value to 0 is equal to boolean false
//applied to if statement. There is no diff between integer 0 or string '0'
if($pos = 0){
$pos++;
} else {
$pos = 0;
}
var_dump($pos);
//$pos is not boolean false, so it should do if condition true, but it's not
//to make this work well i use this
$pos = 0;
if($pos !== false){
$pos++;
} else {
$pos = 0;
}
var_dump($pos);
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74871&edit=1