Php 0, null, empty, null, false, detailed description of the string relationship. I encountered a strange problem in a project, and it took me a lot of time to solve it. The final debugging found that it was a judgment problem-about 0 and ''(null single quotes, I encountered a strange problem in a project. it took me a lot of time to solve it, the final debugging found that it was a matter of judgment-with regard to the judgment of 0 and ''(null single quotes, I added a space for better understanding), I found that 0 =" was actually true, when depressed, I decided to write a simple page test and vowed to clarify the relationship between 0, null, empty, null, and false. This is probably because there are bugs in the programs we write and websites in some key areas. In particular, it may affect the security and logic correctness of login and other special places. Although this is a basic knowledge point, many people, including some experts, may be 0, null, empty, empty, the relationship between false and false is vague. Therefore, it is quite necessary to manually perform a test.
Php code:
The code is as follows:
// ========== Judge the relationship between 0 and ''and empty null false start ================ //
If ('safdasefasefasf' = 0 ){
Echo "this string is converted to a number equal to 0
";
} // Output: the string is converted to a value equal to zero.
This is a key example.
This value is determined by the first part of the string. If a string starts with a valid number, use this number as its value. Otherwise, its value is 0 (0 ).
That is to say, '3asfdf '= 3; 'adsfasdf' = 0.
$ A = 0;
If ($ a = ''){
Echo "0 equals''
";
} // Output: 0 equals''
If (trim ($ a) = ''){
Echo "trim (0) equals''
";
} // No output
If ($ a = ''){
Echo "0 =''
";
} // No output
If (empty ($ )){
Echo "'' is empty
";
} // Output: ''is empty
If (is_null ($ )){
Echo "0 is null
";
} // No output
If (is_numeric ($ )){
Echo "0 is numeric
";
} // Output: 0 is numeric
If (is_string ($ )){
Echo "0 is string
";
} // No output
If (strval ($ a) = ''){
Echo "convert to string 0 is''
";
} // No output
// ========== Judge the relationship between 0 and ''and empty null false end ============== //
// ============ Judge the relationship between ''and 0 and empty null false start ============== //
$ B = '';
If ($ B = 0 ){
Echo "'' equals 0
";
} // Output: ''equals 0
If (! ''){
Echo "'' is false.
";
} // Output: ''is false.
If (! 0 ){
Echo "0 is false
";
} // Output: 0 is false
// ============ Judge the relationship between ''and 0 and empty null false end ============== //
Echo "when judging null (''), be careful. 0 is equivalent to '', 0 and'', both are equivalent to null and false, it is best to use = ";
?>
The output result is: 0 is equal to "is empty 0 is numeric" is equal to 0 "is false 0 is false when determining null ("), be careful, and 0 is equivalent ", both 0 and "are equivalent to null characters and false. it is best to use = to judge as null. it can only be interpreted as follows: 0 is also equivalent to", 0 and "are equivalent to null characters and false
Be careful when determining the null ("). 0 is equivalent to", 0 and "are equivalent to null characters and false. it is best to use = to judge the null;
Else ''(empty single quotes, for better understanding I add...