PHP 0,null,empty, NULL, FALSE, string relationship details _php tips

Source: Internet
Author: User
Encountered a strange problem in a project that cost me a lot of time without solving final debugging discovery is the question of judgment--about 0 and ' (empty single quotes, for good see I add a space) judgment, I found 0== "incredibly set up, depressed at the same time decided to write a simple page test, vowed to 0,null , empty, empty, false relationships are clear. Because it's likely that in some key places we write programs that have bugs in the Web site. In particular, may affect the landing and other special places of security and logic correctness, although this is a very basic knowledge point, but many people, including some experts may be 0,null,empty, empty, false relationship is very vague. So it is very necessary to do a test yourself.
PHP Program code:
Copy Code code as follows:

<?php
//========= determines the relationship between 0 and "and empty null false" start =========//
if (' safdasefasefasf ' ==0) {
echo "Converts the string to a number equal to 0 <br/>";
}//output: The string is converted to a number equal to zero.
This is one of the key examples
The manual explains: The value is determined by the first part of the string. If the string starts with a valid numeric data, the number is used as its value, otherwise the value is 0 (0).
That is to say ' 3asfdf ' ==3; ' Adsfasdf ' ==0 quite to notice
$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 ($a)) {
echo "' is empty <br/>";
}//output: ' Is empty
if (Is_null ($a)) {
echo "0 is null <br/>";
}//no Output
if (Is_numeric ($a)) {
echo "0 is numeric <br/>";
}//output:0 is numeric
if (is_string ($a)) {
echo "0 is string <br/>";
}//no Output
if (strval ($a) = = ") {
echo "converts to a string of 0 is ' <br/>";
}//no Output
========= determines the relationship end of 0 and ' and empty null false ' =========//
========= 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 It's false.
========= The relationship end of ' and 0 and empty null false =========//
echo "In Judging empty (") must be careful, 0 also equivalent to ", 0 and" are equivalent to null characters and false, judged to be empty best use = = ";
?>

The result of the output is: 0 equals "" is empty 0 is numeric "equals 0" or false 0 is false in the judgment empty (") must be careful, 0 also corresponds to", 0 and "are equivalent to null characters and false, judge is empty best use = = This can only be explained: 0 is also equivalent to ", 0 and" are equivalent to null characters and False
In the judgement of empty (") must be careful, 0 also equivalent to", 0 and "are equivalent to null characters and false, judged to be empty best use = = =;

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.