Home  >  Article  >  Backend Development  >  How to check if a variable is set in php

How to check if a variable is set in php

青灯夜游
青灯夜游Original
2021-09-30 16:56:222417browse

In PHP, you can use the isset() function to check whether a variable is set. This function can be used to detect whether the variable has been set and is not NULL. The syntax is "isset ($var)"; if you specify a variable Returns TRUE if set, otherwise returns FALSE.

How to check if a variable is set in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php checks a variable Whether it is set

Use the isset() function, which can detect whether the variable has been set and is not NULL.

Syntax

bool isset ( mixed $var [, mixed $... ] )

Parameter description:

  • $var: the variable to be detected.

If multiple parameters are passed in at one time, isset() will only return TRUE when all parameters are set. The calculation process is from left to right, and when an unset variable is encountered midway will stop immediately.

Return value: TRUE if the specified variable exists and is not NULL, otherwise FALSE.

Example:

<?php
$var = &#39;&#39;;
 
// 结果为 TRUE,所以后边的文本将被打印出来。
if (isset($var)) {
    echo "变量已设置。" . PHP_EOL;
}
 
// 在后边的例子中,我们将使用 var_dump 输出 isset() 的返回值。
// 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
?>

The output result is:

变量已设置。
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)

Explanation: If a variable has been released using unset(), and then judged by isset() Return FALSE.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to check if a variable is set in php. For more information, please follow other related articles on the PHP Chinese website!

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