Home  >  Article  >  Backend Development  >  Judgment of parameter structure by empty and isset in PHP and the difference between empty() and isset(), emptyisset_PHP tutorial

Judgment of parameter structure by empty and isset in PHP and the difference between empty() and isset(), emptyisset_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:04:37775browse

The judgment of empty and isset on parameter structure in PHP and the difference between empty() and isset(), emptyisset

No more nonsense, I will just post the code for you.

<&#63;php
  class test{}
  $a1 = null;
  $a2 = "";
  //$a3 =
  $a4 = 0;
  $a5 = '0';
  $a6 = false;
  $a7 = array();
  //var $a8;
  $a9 = new test();
  for ($i=1; $i <=9 ; $i++) {
    $s = 'a'.$i;
    echo $i . ":";
    var_dump(isset($$s));
    echo '<br />';
  }
  echo '<br />';
  for ($i=1; $i <=9 ; $i++) {
    $s = 'a'.$i;
    echo $i . ":";
    var_dump(empty($$s));
    echo '<br />';
  }

PS: The difference between empty() and isset() in PHP

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:

can determine whether a variable is empty;

Both return boolean type, that is, true or false.

The following is a detailed description of the differences between their usage:

isset() is used to check whether a variable is set and can only be used for variables, because passing any other parameters will cause a parsing error. If you want to detect 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 the NULL byte ("

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

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

<&#63;php
     var $a=0;
      //empty($a)返回true
      if(empty($a)){
         echo "判断结果是空"
      }
     //isset($a)返回true
      if(isset($a)){
         echo "判断结果不是空"

      }
    &#63;>

http://www.bkjia.com/PHPjc/1072193.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1072193.htmlTechArticleThe judgment of empty and isset on parameter structure in PHP and the difference between empty() and isset(), emptyisset is nonsense Without further ado, I’ll just post the code for you guys. php class test{} $a1 = null; $a2 = "";...
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