Home > Backend Development > PHP Tutorial > Detailed introduction to the relationship between 0, null, empty, empty, false, and strings in PHP_PHP Tutorial

Detailed introduction to the relationship between 0, null, empty, empty, false, and strings in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:04:42
Original
894 people have browsed it

I encountered a strange problem in a project, and it took me a lot of time to solve it. In the end, debugging found that it was a judgment problem - about 0 and ' ' (empty single quotes, I added a space for better visibility) Judgment, I found that 0==" was actually established. I was depressed and decided to write a simple page test. I vowed to make the relationship between 0, null, empty, empty, and false clear. Because this is likely to be used in some key places. There are bugs in the programs and websites we write, especially those that may affect the security and logical correctness of special places such as login. Although this is a very basic knowledge point, many people, including some experts, may be confused about 0, null, and empty. The relationship between , empty and false is very vague. So it is still necessary to do a test yourself.
php program code:

Copy code The code is as follows:

//========== Determine the relationship between 0 and '' and empty null false start =========//
if('safdasefasefasf'==0){
echo "The string is converted into a number equal to 0
";
}//output: The string is converted to a number equal to zero.
This is a key example.
The manual explains: The value is determined by the first part of the string. If The string starts with legal numeric data, and uses that number as its value, otherwise its value is 0 (zero)
That is to say '3asfdf'==3; 'adsfasdf'==0 is quite necessary. Note that
$a=0;
if($a==''){
echo "0 is equal to ''
";
} / /output:0 is equal to ''
if(trim($a)==''){
echo "trim(0) is equal to''
";
} //no output

if($a===''){
echo "0===''
";
} //no output
if( empty($a)){
echo "'' is empty
";
} //output:'' is empty
if(is_null($a)){
echo "0 is null
";
} //no output
if(is_numeric($a)){
echo "0 is numeric
";
} //output:0 is numeric
if(is_string($a)){
echo "0 is string
";
} //no output
if(strval($a)==''){
echo "0 converted into a string is ''
";
} //no output
//== ======= Determine the relationship between 0 and '' and empty null false end =========//
//========== Determine the relationship between '' and 0 and empty null false relationship start =========//
$b = '';
if($b==0){
echo "'' is equal to 0
";
} //output:'' is equal to 0
if(!''){
echo "'' is false
";
} // output:'' is false
if(!0){
echo "0 is false
";
} //output:0 is false
//=== ====== Determine the relationship between '' and 0 and empty null false end =========//
echo "Be careful when judging empty (''), 0 is also equivalent For '', 0 and '' are equivalent to the empty character and false. It is best to use ===";
?>

to judge if it is empty. The output result is: 0 is equal to " ” is empty 0 is numeric ” is equal to 0 ” is false 0 is false Be careful when judging empty ("), 0 is also equivalent to " , 0 and " are equivalent to empty characters and false, it is best to judge empty === can only be explained this way: 0 is also equivalent to " , 0 and " are both equivalent to the empty character and false
Be careful when judging the empty character ("), 0 is also equivalent to " , 0 and " are equivalent For empty characters and false, it is best to use ===;
if it is judged to be empty.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327751.htmlTechArticleI encountered a strange problem in a project. It took me a lot of time to solve it. Finally, I found out during debugging It's a matter of judgment - about 0 and ' ' (empty single quotes, I added them for better clarity...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template