Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var_dump(0 == 'all'); // WHY TRUE

Tags:

php

I don't understand below output . found below expressions on php.net manual in boolean section.

<?php

    var_dump(0 == 'all');//   IS bool(true)
    var_dump((string)0 == 'all');  //IS bool(false)
    var_dump(0 === 'all'); // //IS bool(false)

?>
like image 650
ajax D Avatar asked Dec 30 '14 10:12

ajax D


People also ask

What is the use of Var_dump ()?

Definition and Usage The var_dump() function dumps information about one or more variables. The information holds type and value of the variable(s).

What is the difference between Var_dump and Print_r?

var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.

Where is Var_dump output?

We have already learned that var_dump() function is used to display structured information (type and value) about one or more expressions. The function outputs its result directly to the browser.


1 Answers

If you compare an integer with a string, each string is converted to a number, so:

(0 == 'all') -> (0 == 0) -> true

The type conversion does not happen when the comparison is === or !== because this also includes the comparison of the type:

(0 === 'all') -> (integer == string) -> false

The second line of code you wrote force the integer value to be considered as a string, and so the numerical cast doesn't happen.

like image 157
Giulio Biagini Avatar answered Sep 23 '22 20:09

Giulio Biagini