Home >Backend Development >PHP Tutorial >A brief analysis of the usage of php isset() and unset() functions

A brief analysis of the usage of php isset() and unset() functions

WBOY
WBOYOriginal
2016-07-25 08:53:481534browse
  1. $var = '';
  2. // The result is true, so the following text will be printed.
  3. if (isset($var)) {
  4. print "this var is set set so i will print.";
  5. }
  6. // In the following example, we will use var_dump to output the return value of isset().
  7. $a = "test";
  8. $b = "anothertest";
  9. var_dump( isset($a) ); // true
  10. var_dump( isset ($a, $b) ); // true
  11. unset ($a );
  12. var_dump( isset ($a) ); // false
  13. var_dump( isset ($a, $b) ); // false
  14. $foo = null;
  15. var_dump( isset ($foo) ); // false
  16. ?>
Copy code

This also works for elements in an array:

  1. $a = array ('test' => 1, 'hello' => null);
  2. var_dump( isset ($a['test']) ); // true
  3. var_dump( isset ($a['foo']) ); // false
  4. var_dump( isset ($a['hello']) ); // false
  5. // The value of key 'hello' is equal to null, so Considered unset.
  6. // If you want to detect null key values, you can try the method below.
  7. var_dump( array_key_exists('hello', $a) ); // true
  8. ?>
Copy code

Note: Since this is a language structure rather than a function, it cannot be called by "variable functions" . Reasonable application of the php function isset() can help us detect whether the variable is set. Returns false if the variable does not exist, and true if the variable exists and is not null. Through studying 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. Down

Related 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 Also returns false if the variable exists and its value is null If the variable exists and the value is not null, return true When checking multiple variables at the same time, true will be returned when each single item meets the previous requirement, otherwise the result will be false Version: php 3, php 4, php 5

More instructions: After a variable is freed using unset(), it is no longer isset(). The php function isset() can only be used for variables. Passing any other parameters will cause a parsing error. Check whether a constant has been set using 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.



Statement:
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