Detailed explanation of the difference between isset and empty in php

小云云
Release: 2023-03-21 10:18:02
Original
2093 people have browsed it

1. For those who are new to PHP, it is difficult to figure out the difference between the usage of empty() and isset(). It is indeed difficult to figure out the difference in their usage without careful consideration.

Let’s talk about what they have in common:

  1. can both determine whether a variable is empty;

  2. both return boolean Type, i.e. true or false.

Let’s talk about the difference between their usage in detail:

  1. isset() is used to detect whether the variable is set, and can only be used for variables , because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, use the defined() function. If a variable has been freed using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. (Note that a NULL byte ("\0") is not equivalent to PHP's NULL constant)

  2. empty() is used to check whether a variable is empty.

  3. The biggest difference between them is the judgment of 0. If you use empty to judge, it will be considered to be empty, and if you use isset, it will be considered not to be empty. For example:

<?php
          var $a=0;
           //empty($a)返回true
           if(empty($a)){
                 echo "判断结果是空"
           }
          //isset($a)返回true
           if(isset($a)){
                 echo "判断结果不是空"
           }
       ?>
Copy after login

Related recommendations:

Have questions about isset and empty?

The difference between isset and empty_PHP tutorial

Example study on the difference between PHP function isset and empty

The above is the detailed content of Detailed explanation of the difference between isset and empty in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!