Home >Backend Development >PHP Tutorial >Usage of php isset function

Usage of php isset function

WBOY
WBOYOriginal
2016-07-25 09:11:481589browse

isset function checks whether the variable is set.

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

Return value:

If the variable does not exist, return FALSE Also returns FALSE if the variable exists and its value is NULL If the variable exists and the value is not NULL, TRUE is returned When checking multiple variables at the same time, TRUE is returned only when each single item meets the previous requirement, otherwise the result is FALSE. If a variable has been freed 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 parameters will cause a parsing error. If you want to detect whether a constant has been set, use the defined() function.

Example 1:

  1. $var = '';
  2. if (isset($var)) {
  3. print "This var is set set so I will print.";
  4. }// In the following example , we will use the var_dump function to output the return value of isset().
  5. $a = "test";
  6. $b = "anothertest";
  7. var_dump( isset($a) ); // TRUE
  8. var_dump( isset ($a, $b) ); // TRUE
  9. unset ($a );
  10. var_dump( isset ($a) ); // FALSE
  11. var_dump( isset ($a, $b) ); // FALSE
  12. $foo = NULL;
  13. var_dump( isset ($foo) ); // FALSE
  14. ?>
Copy code
Example 2:

  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. // 'hello' is equal to NULL, so it is considered unassigned.

  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

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