PHP: Understanding string type juggling

痞子三分冷 提交于 2020-03-04 08:45:29

问题


If I set $var to a string in PHP, that variable will evaluate to true in any conditions:

$var = "foo";
if ( $var ) {
    echo 'I will be printed';
} else {
    echo 'I will not be printed';
}

I understand that my condition above automatically does type juggling so that $var is converted in a bool.

Now, if I cast $var to an integer, I get 0:

var_dump( (int)$var ); // int(0)

0 is a falsy value which becomes false when converted to a bool:

$zero = 0;
var_dump( (bool)$zero ); // bool(false)

Considering the above, why doesn't my condition print "I will not be printed"?


回答1:


Type juggling isn't lossless. It comes with potential loss of data.

For instance...

var_dump( strval(intval("foo"))); // string(1) "0"

But if you were to write...

if( "foo" == "0") // non-strict comparison intended

You would surely not expect it to run! And indeed it doesn't.

So because type changes come with data loss, you can't expect conversions to be equivalent. In other words, boolval(intval($var)) and boolval($var) need not be the same.

If you want to be strict in your comparisons, use something like this:

if( is_string($var) && $var !== "") // is not the empty string

(Arguably !== is redundant and != works fine, but arguably this whole condition is overkill)




回答2:


Here is a simple test of truth:

$s = array( // bool  (int)cast
    '',     // FALSE int=0
    'Test', // TRUE  int=0
    '0.0',  // TRUE  int=0
    '124',  // TRUE  int=124
    '000',  // TRUE  int=0
    '0',    // FALSE int=0
    NULL    // FALSE int=0
);


foreach( $s as $var )
    echo $var . ' // ' 
        . ( $var ? 'TRUE' : 'FALSE' ) . ' '
        . 'int=' . (int)$var . PHP_EOL;

In case of a string casting to bool, FALSE is an empty string, NULL and the value '0'. Everything else is TRUE. See the manual on false.

In your case, $var is a string "foo", which is not being converted to FALSE since it is neither NULL, "0" or "", therefore you will not get 'I will not be printed'.

If you cast it to an INT, everything is 0 except a pure numerical value.




回答3:


You will not get the else echo done, as a string that is not empty evaluates to true when used in an if, i.e.:

$foo = "Hello";
if ($foo)
    print "True"; //This will print as a string is true if not empty
else 
    print "False"; //Doesn't print as string is not empty

Try this:

$foo = "Hello";
if ((int)$foo)
    print "True";
else
    print "False"; //Will print as the string evaluates to 0 (false) as it is not an integer


来源:https://stackoverflow.com/questions/34044348/php-understanding-string-type-juggling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!