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, for better understanding, I added a space), I found that 0 = "was actually true, depressed at the same time decided to write a simple page test, swear to 0, null, empty, the relationship between null and false is clear. Because it is likely to be written in some key areasProgramThe website has a bug. 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 ProgramCode:
<? PHP // ========== 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 <br/>" ;}// output: This string is converted to a number equal to zero.
This is a key example.
The manual provides the following explanations: 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 ''<br/> ";}// Output: 0 equals''If (TRIM ($ A) = '') {echo" Trim (0) equals ''<br/> ";}// No output
If ($ A = '') {echo" 0 = ''<br/> ";}// No outputIf (empty ($ A) {echo "'' is empty <br/> ";}// Output: ''is emptyIf (is_null ($ A) {echo "0 is null <br/> ";}// No outputIf (is_numeric ($ A) {echo "0 is Numeric <br/> ";}// Output: 0 is NumericIf (is_string ($ A) {echo "0 is string <br/> ";}// No outputIf (strval ($ A) = '') {echo" convert to string 0 is ''<br/> ";}// 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 <br/> ";}// Output: ''equals 0If (! '') {Echo" ''is false <br/> ";}// Output: ''is false.If (! 0) {echo "0 is false <br/> ";}// 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 =It can only be interpreted as follows: 0 is 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;