The difference between empty and isset in PHP for parameter structure and empty () and isset () _php instance

Source: Internet
Author: User
Tags constant parse error

No more nonsense to say, directly to everyone paste code.

<?php
  class test{}
  $a 1 = null;
  $a 2 = "";
  $a 3 =
  $a 4 = 0;
  $a 5 = ' 0 ';
  $a 6 = false;
  $a 7 = array ();
  var $a 8;
  $a 9 = 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/> ';
  }

The difference between empty () and Isset () in ps:php

For beginners in PHP, the difference between empty () and isset () is hard to figure out, and it's really hard to figure out what their differences are.

Let's say what they have in common:

Can be used to determine whether a variable is empty;

Returns a Boolean type, that is true or false.

Here's a specific distinction between their usage:

Isset () is used to detect whether a variable is set and can only be used for a variable, because passing any other parameter will result in a parse error. To detect if a constant is set, use the defined () function. If you have freed a variable using unset (), it will no longer be isset (). If you use Isset () to test a variable that is set to NULL, it returns FALSE. (Note that a null byte ("") is not equivalent to the null constant of PHP)

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

The biggest difference between them is that for 0 of the judgment, if the empty judge will be considered empty, with isset is considered not empty, for example:

<?php
     var $a =0;
      Empty ($a) returns True
      if (empty ($a)) {
         echo evaluates to null
      }
     //isset ($a) returns True
      if (isset ($a)) {
         echo " Judging result is not empty "

      }
    ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.