Differences between empty () and isset () in PHP _ PHP Tutorial

Source: Internet
Author: User
The difference between empty () and isset () in PHP. Differences between empty () and isset () in PHP 1. purpose of the empty function: checks whether the variable is null. If var is not null or a non-zero value, empty () returns FALSE. In other words, the difference between empty () and isset () in PHP, 0, 0
1. empty function

Purpose: Check whether the variable is empty.

Judgment: If var is a non-null or non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attributes will be considered empty, if var is null, TRUE is returned. SOURCE Manual: http://php.net/manual/zh/function.empty.php

Note: empty () only detects variables, and any non-variables can cause parsing errors.

$ Name = 0;

$ Name = '';

$ Name = null;

$ Name = "0 ";

$ Name;

If (empty ($ name ))

{

Echo "OK"; // empty ($ name) in the preceding five statements are true.

}

If (empty ($ na) // The result is true because the variable $ na is not defined

2. isset functions

Purpose: Check whether the variable is set.

Judge: Check whether the variable is set and not NULL. 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.

The isset function is used to check whether this variable has been set. in the following two cases, isset is considered false.

① $ Id; no value is given

② Or there is no definition at all.

Therefore, we recommend that you determine whether a form has submitted data and use isset to determine if (isset ($ _ GET/$ _ POST ['variable']). {echo $ _ GET ['variable']} else {echo 'no data passed '}

Example:

$ A1 = null;

$ A2 = false;

$ A3 = 0;

$ A4 = '';

$ A5 = '0 ';

$ A6 = 'null ';

$ A7 = array ();

$ A8 = array ());

Echo empty ($ a1 )? 'True': 'false'; // outputs true

Echo empty ($ a2 )? 'True': 'false'; // outputs true

Echo empty ($ a3 )? 'True': 'false'; // outputs true

Echo empty ($ a4 )? 'True': 'false'; // outputs true

Echo empty ($ a5 )? 'True': 'false'; // outputs true

Echo empty ($ a6 )? 'True': 'false'; // The output is false.

Echo empty ($ a7 )? 'True': 'false'; // outputs true

Echo empty ($ a8 )? 'True': 'false'; // The output is false.

Echo'
';

Echo isset ($ a1 )? 'True': 'false'; // The output is false.

Echo isset ($ a2 )? 'True': 'false'; // outputs true

Echo isset ($ a3 )? 'True': 'false'; // outputs true

Echo isset ($ a4 )? 'True': 'false'; // outputs true

Echo isset ($ a5 )? 'True': 'false'; // outputs true

Echo isset ($ a6 )? 'True': 'false'; // outputs true

Echo isset ($ a7 )? 'True': 'false'; // outputs true

Echo isset ($ a8 )? 'True': 'false'; // outputs true

1. if the isset value is not assigned a value to the variable or the value is NULL, false is determined. the other values are true;

2. empty requires a lot of attention and should be determined based on the definition of the function.

Difference between values () and isset () 1. empty function purpose: checks whether the variable is null. Judgment: If var is a non-empty or non-zero value, empty () returns FALSE. In other words, 0, 0 ,...

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.