Differences between isset (variable) and direct variable determination in PHP-php Tutorial

Source: Internet
Author: User
In PHP, the difference between isset (variable) and direct variable judgment is that when the variable is a null string, boolean value false, and numeric value 0, isset returns true for all, and directly returns false for all:

$ Empty = ''; echo (isset ($ empty )? '1': 0); // 1 echo'
'; Echo ($ empty? '1': 0); // 0
$ Empty = false; echo (isset ($ empty )? '1': 0); // 1 echo'
'; Echo ($ empty? '1': 0); // 0
$ Empty = 0; echo (isset ($ empty )? '1': 0); // 1 echo'
'; Echo ($ empty? '1': 0); // 0

They are the same only when the variables are null:

$ Empty = null; echo (isset ($ empty )? '1': 0); // 0 echo'
'; Echo ($ empty? '1': 0); // 0

Although isset does not have any function in judging variables, it is very useful in judging whether an array has one variable:

$ Arr = array ('1' => 1, '3' => 3); $ temp = null; // PS: arrays in php are different from those in map, if there is no key and you go to the value, it will lead to an error instead of returning null. Therefore, a corresponding judgment should be made first, and then the value if (isset ($ arr ['2']). {// The effect is equivalent to array_key_exists ('2', $ arr), but the code is shorter than $ temp = $ arr ['2'];} echo $ temp;

If you do not know about isset, you can go to the official website to see it:
Http://php.net/manual/zh/function.isset.php

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.