Home > Backend Development > PHP Tutorial > Summary of usage of isset() and unset() functions in PHP_PHP Tutorial

Summary of usage of isset() and unset() functions in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:36:12
Original
900 people have browsed it

isset
(PHP 3, PHP 4, PHP 5)

isset -- Check whether the variable is set

Description
bool isset ( mixed var [, mixed var [, ...]])
Returns TRUE if var exists, FALSE otherwise.

If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("

Warning: isset() can only be used with variables, as passing any other arguments will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.


Copy code The code is as follows:
$var = '';

// The result is TRUE, so the following text will be printed.

if (isset($var)) {
print "This var is set set so I will print.";
}

// In the following examples, 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 also works for elements in an array:

Copy code The code is as follows:
$a = array ('test' => 1, 'hello' => NULL);
var_dump( isset ($a['test']) );                                                                                                                                                      'hello']) );                   // FALSE


// The value of key 'hello' is equal to NULL, so it is considered unset.
// If you want to detect NULL key values, you can try the method below.

var_dump( array_key_exists('hello', $a) ); // TRUE

?>


Note: Since this is a language structure rather than a function, it cannot be "Variable function" call.

Proper application of the PHP function isset() can help us detect whether a variable is set. Returns FALSE if the variable does not exist, and TRUE if the variable exists and the value is not NULL.
Through learning the PHP language, you should know that it is a function-based HTML scripting language. A huge function library supports the implementation of PHP language functions. Below we will introduce to you the relevant usage of the PHP function isset().

Format: bool isset ( mixed var [, mixed var [, ...]] )

Function: Detect whether a variable is set

Return value:

If the variable does not exist, it returns FALSE

If the variable exists and its value is NULL, it also returns FALSE

If the variable exists and its value is not NULL, it returns TURE


When checking multiple variables at the same time, TRUE will be returned only when each single item meets the previous requirement, otherwise the result will be FALSE

Versions: PHP 3, PHP 4, PHP 5

More instructions:

After a variable is released using unset(), it will no longer be isset().

The PHP function isset() can only be used for variables. Passing any other parameters will cause a parsing error.

To check whether a constant has been set, use the defined() function.



unset()

Destroy the specified variable. Note that in PHP 3, unset() will return TRUE (actually the integer value 1), while in PHP 4, unset() is no longer a real function: it is now a statement. There is no return value, and trying to get the return value of unset() will result in a parsing error.

http://www.bkjia.com/PHPjc/740215.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/740215.htmlTechArticleisset (PHP 3, PHP 4, PHP 5) isset -- Check whether the variable is set. Description bool isset (mixed var [, mixed var [, ...]]) Returns TRUE if var exists, FALSE otherwise. If already...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template