Php 0, null, empty, null, false, detailed description of the string relationship

Source: Internet
Author: User

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. 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:
Copy codeThe Code is as follows: <? 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: 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'' <br/> ";
} // Output: 0 equals''
If (trim ($ a) = ''){
Echo "trim (0) equals'' <br/> ";
} // No output

If ($ a = ''){
Echo "0 ='' <br/> ";
} // No output
If (empty ($ )){
Echo "'' is empty <br/> ";
} // Output: ''is empty
If (is_null ($ )){
Echo "0 is null <br/> ";
} // No output
If (is_numeric ($ )){
Echo "0 is numeric <br/> ";
} // Output: 0 is numeric
If (is_string ($ )){
Echo "0 is string <br/> ";
} // No output
If (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 0
If (! ''){
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 = 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.