Home > Article > Backend Development > What is the difference between php false and 0
php The difference between false and 0: 1. False is a boolean value, and 0 is the value 0; 2. In PHP, false is stored with a value of 0, but the types of false and 0 are different.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3.
What is the difference between false and 0 in php?
The difference between 0 and Null false in php
<?php $test=0; if($test==''){ echo '<br />在php中,0即为空'; //被输出 } if($test===''){ echo '<br />在php中,0即为空'; //不被输出 } if($test==NULL){ echo '<br />在php中,0即为空'; //被输出 } if($test===NULL){ echo '<br />在php中,0即为空'; //不被输出 } if($test==false){ echo '<br />在php中,0即为空'; //被输出 } if($test===false){ echo '<br />在php中,0即为空'; //不被输出 } ?>
The reason is that in PHP the variable is Stored in C language structures, empty strings, NULL, and false are all stored with a value of 0. This structure has a zend_uchar type; such a member variable is used to save the type of the variable. The type of empty string is string, the type of NULL is NULL, and false is boolean.
You can use echo gettype(''); and echo gettype(NULL); to print this! The === operator not only compares values, but also compares types, so the third is false!
So it can be said that === is equal to the following function:
function eq($v1, $v2) { if($v1 == $v2 && gettype($v1) == gettype($v2)) { return 1; } else { return 0; } }
So the empty string (''), false, NULL and 0 are equal in value but different in type !
Note:
NULL is a special type.
It is NULL in two cases
1. $var = NULL;
2. $var;
3."", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty , if var is empty, returns TRUE.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between php false and 0. For more information, please follow other related articles on the PHP Chinese website!