Determination of parameter structure by empty and isset in PHP and difference between empty () and isset () _ php instance

Source: Internet
Author: User
This article describes how to judge the parameter structure of empty and isset in PHP and the differences between empty () and isset (). If you need more information, refer to the following, paste the Code directly.

<?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 '
'; } echo '
'; for ($i=1; $i <=9 ; $i++) { $s = 'a'.$i; echo $i . ":"; var_dump(empty($$s)); echo '
'; }

PS: differences between empty () and isset () in PHP

For beginners of php, the difference between empty () and isset () is difficult to figure out. It is really difficult to figure out the difference between their usage without careful consideration.

Let's talk about what they have in common:

You can determine whether a variable is null;

Return the boolean type, that is, true or false.

The differences between their usage are as follows:

Isset () is used to check whether a variable is set. It can only be used for a variable because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () function. If unset () is used to release a variable, it will no longer be isset (). If you use isset () to test a variable that is set to NULL, FALSE is returned. (Note that a NULL byte ("\ 0") 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 the determination of 0. If empty is used, it is considered to be null. If isset is used, it is considered not null. For example:

<? Php var $ a = 0; // empty ($ a) returns true if (empty ($ a) {echo "The result is null"} // isset ($) returns true if (isset ($ a) {echo "the 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.