Home  >  Article  >  Backend Development  >  In php, 0 == false, 0 == false will be equal, and false != false is the reason.

In php, 0 == false, 0 == false will be equal, and false != false is the reason.

WBOY
WBOYOriginal
2016-07-28 08:27:142299browse

First look at a special case:

var_dump(0 == 'false'), returns bool(true), PHP determines that 0 is equal to any string;

var_dump(0==='false'), returns bool( false), the types and values ​​must be equal if they are congruent.
var_dump(0 == false), returns bool(true),


The reasons for the above results (translated from php official documentation):

1. For bool type:

When you output the bool type or When used in a statement, it will be converted into a number , true becomes 1, and false becomes 0.

For example, $a = true; var_dump($a+1), returns (int)2; $a is converted to 1, and the sum is 2;

A bool variable expresses a true value, and Rather than expressing a 0 or 1; Boolean types are not iconic constants, they have values.

2. String type:

php will always automatically try to convert strings into numbers. For example, var_dump('abc'+3'), returns int(3),

First 'abc' is converted into the number 0, and the addition becomes 3;


Method to detect the type of variable:
1. var_dump ($param) , will return the type and value of the variable.

2. Use is_int(), is_bool(), is_string(); functions to return true and false,


commonly used type conversion methods.
Variable $param

Integer type (int)$param

Character type (string)$param

Boolean type (bool)$param

Another way to convert to Boolean type: $a = !5, $a is false ,$a = !!5,$a is true;


The above introduces the reasons why 0 == false, 0 == false will be equal, and false ! = false in php. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn