What is the difference between 0, empty, null and false in php (code example)

不言
Release: 2023-04-04 07:10:01
Original
1811 people have browsed it

The content of this article is about the difference between 0, empty, null and false in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

$a =  0; 
$b="0"; 
$c= ''; 
$d= null; 
$e = false;
 
echo "5个变量-原始测试类型";
    var_dump($a);//int 0
    var_dump($b);//string '0'
    var_dump($c);//string ''
    var_dump($d);//null
    var_dump($e);//boolean false
 
echo "<h4>empty测试</h4>";
    var_dump(empty($a));//true
    var_dump(empty($b));//true
    var_dump(empty($c));//true
    var_dump(empty($d));//true
    var_dump(empty($e));//true
 
echo "<hr>";
    var_dump(isset($a));//true
    var_dump(isset($b));//true
    var_dump(isset($c));//true
    var_dump(isset($d));//【false】 见结论一
    var_dump(isset($e));//true
 
echo "<h4>(==)双等式测试</h4>";
    var_dump($a == $b);//true
    var_dump($a == $c);//true
    var_dump($a == $d);//true
    var_dump($a == $e);//true !!
 
    var_dump($b == $c);//【false】见结论二
    var_dump($b == $d);//【false】见结论二
    var_dump($b == $e);//true
 
    var_dump($c == $d);//true
    var_dump($c == $e);//true
 
echo "<h4>(===)三等式测试</h4>";
    var_dump($a === $b);//false
    var_dump($a === $c);//false
    var_dump($a === $d);//false
    var_dump($a === $e);//false
 
    var_dump($b === $c);//false
    var_dump($b === $d);//false
    var_dump($b === $e);//false
 
    var_dump($c === $d);//false
    var_dump($c === $e);//false
Copy after login

Summary:

For [0;'0' ;''; null false】Five types

empty operates the above five variables and all returns false

Strongly equal to (=== ) comparisons are all false, which is the same as the strong language result (three equal signs comparison not only needs to compare values, but also types)

But for (==) comparison, it is necessary Pay attention to the string type, which involves the underlying structure and type conversion

Conclusion 1: Understanding of variable types

1.null means non-existence: in the zval space at the bottom of PHP (see the structure below ) does not store its value, but only stores a type flag IS_NULL (so it explains empty(null)=true, isset(null)=false, isset('')=true)

2.【 0 ; '0' ; '' ; false ]: These four exist. The bottom layer of php is to open up zval space storage. There is value and type

Conclusion 2:

1. String '0' is not equal to string '' (you will understand after thinking about it) , comparing the same type, how can a string of [1 length] be equal to a string of [0 length])

2, int 0 is the same as string '' Null equality, (non-identical comparison, PHP will do type conversion)

3, string '0' is not equal to null, int 0 is equal to null

Related recommendations:

The difference between 0 and null and false and empty in php

The above is the detailed content of What is the difference between 0, empty, null and false in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!