Home  >  Article  >  Backend Development  >  In PHP, the difference between 0 and null

In PHP, the difference between 0 and null

怪我咯
怪我咯Original
2017-01-16 13:49:512187browse

In PHP, the difference between 0 and null

I don’t know that when you store fields with default data of 0 in the MySQL database, but there is an error when judging 0 or Null in the program, because 0 and Null is not completely equal. It seems like a simple problem, but it is very tricky to judge. The values ​​of Null and 0 are the same, but the types are not the same. To put it simply, it is the difference between == and ===. The first == only needs the same value, but === not only requires the same value but also the same type of value.

Null is a special type, two types are Null, such as the following code:

1. $var = NULL;

2. $var;

3.0, "0", NULL, and objects without any attributes will be considered empty.

Look at the example below

<?
 
$str1 = null;
 
$str2 = 0;
 
echo $str1==$str2 ? ‘相等’ : ‘不相等’;
 
$str3 = "";
 
$str4 = 0;
 
echo $str3==$str4 ? ‘相等’ : ‘不相等’;
 
$str5 = 0;
 
$str6 = &#39;0&#39;;
 
echo $str5===$str6 ? ‘相等’ : ‘不相等’;
 
$str7=0;
 
$str= null ;
 
echo $str7==$str8 ? ‘相等’ : ‘不相等’;
 
?>

Run result

'Equal''Equal''Not equal''Equal'

So, 0 and Null The values ​​are equal, but their types are not. Okay, let’s briefly summarize the difference between PHP 0 and null. In short, if you are not clear about the difference, just make a few more judgments.

Statement:
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