Summary of the usage of isset () and unset () functions in PHP _php instance

Source: Internet
Author: User
Tags php language mixed parse error set set

Isset
(PHP 3, PHP 4, PHP 5)

Isset--Detects whether a variable is set

Describe
BOOL Isset (mixed var [, mixed Var [, ...]])
Returns TRUE if VAR exists, otherwise returns FALSE.

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. Also note that a null byte ("" ") is not equivalent to PHP's null constant.

Warning: isset () can only be used for variables because passing any other parameter will result in a parse error. To detect if a constant is set, use the defined () function.

Copy Code code as follows:

<?php
$var = ';

The result is TRUE, so the text behind will be printed.
if (Isset ($var)) {
Print "This var is set set and so I'll print.";
}

In the example below, we will use Var_dump to output the return value of Isset ().

$a = "Test";
$b = "Anothertest";

Var_dump (Isset ($a)); TRUE
Var_dump (Isset ($a, $b)); TRUE

unset ($a);

Var_dump (Isset ($a)); FALSE
Var_dump (Isset ($a, $b)); FALSE

$foo = NULL;
Var_dump (Isset ($foo)); FALSE
?>


This is also valid for elements in the array:
Copy Code code as follows:

<?php
$a = array (' Test ' => 1, ' hello ' => NULL);

Var_dump (isset ($a [' Test '])); TRUE
Var_dump (isset ($a [' foo '])); FALSE
Var_dump (isset ($a [' hello '])); FALSE

The value of the key ' hello ' is equal to NULL, so it is considered an open value.
If you want to detect NULL key values, try the bottom method.
Var_dump (array_key_exists (' hello ', $a)); TRUE
?>


Note: Because this is a language structure and not a function, it cannot be invoked by a "variable function".

Reasonable application of PHP function Isset () can help us to detect whether the variable is set. Returns FALSE if the variable does not exist, and returns ture if the variable exists and the value is not NULL.

By learning about the PHP language, you should know that it is a function based HTML scripting language. The vast library of functions supports the implementation of the PHP language function. Here's a brief introduction to the usage of PHP function isset ().


Format: bool Isset (mixed var [, mixed Var [, ...]])

Function: Detect whether the variable is set

return value:

Returns FALSE if the variable does not exist
Returns FALSE if the variable exists and its value is null
Returns ture if the variable exists and the value is not NULL

When you check multiple variables at the same time, each item returns TRUE when it meets the previous requirement, otherwise the result is FALSE

Version: PHP 3, PHP 4, PHP 5

More Description:

After you use unset () to free a variable, it is no longer a isset ().
php function Isset () can only be used for variables, and passing any other parameter will result in parsing errors.
Detects whether a constant is set to use the defined () function.

Unset ()

Destroys the specified variable. Note in PHP 3, unset () returns True (actually an integer value of 1), whereas in PHP 4, unset () is no longer a real function: it is now a statement. So that there is no return value, trying to get the return value of unset () will result in a parse error.

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.