Home >Backend Development >PHP Tutorial >How to Identify Undefined Variables in PHP?
Identifying Undefined Variables in PHP
In PHP, the isset() function allows you to check if a variable has been set, but it does not distinguish between undefined and null values. To explicitly check for undefined variables, similar to the JavaScript statement document.createTouch !== undefined, you can employ the following approach:
<code class="php">$isTouch = isset($variable);</code>
This expression returns true if $variable is defined, and false otherwise. However, it's important to note that isset() considers a variable defined if it has been set to any value other than NULL.
If you want to check specifically for false, 0, or other values that may be considered "false-like" in PHP, you can use the empty() function:
<code class="php">$isTouch = empty($variable);</code>
empty() returns true for the following cases:
By combining isset() and empty(), you can determine whether a variable is undefined or has a false-like value:
<code class="php">$isTouch = !isset($variable) || empty($variable);</code>
The above is the detailed content of How to Identify Undefined Variables in PHP?. For more information, please follow other related articles on the PHP Chinese website!