"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Check Variable Undefinedness in PHP?

How to Check Variable Undefinedness in PHP?

Published on 2024-11-20
Browse:947

How to Check Variable Undefinedness in PHP?

Determining Variable Undefinedness in PHP

In JavaScript, "document.createTouch !== undefined" checks for the undefinedness of "document.createTouch." Seeking an equivalent in PHP, let's explore ways to determine if a variable is undefined.

Using "isset()"

Unlike JavaScript, PHP does not have an explicit "undefined" keyword. Instead, you can use "isset()" to check if a variable has been defined. It returns true if the variable exists and false otherwise. For example:

$isTouch = isset($variable);

Handling Special Cases

It's important to note that "isset()" returns true even if the variable contains the value NULL. To check if a variable is undefined and not just empty or set to NULL, you can use the following:

if (!isset($variable) || is_null($variable)) {
  // $variable is undefined
}

Using "empty()"

Alternatively, you can use "empty()" to check if a variable is undefined or contains certain values, including the empty string, zero, NULL, and an empty array. However, "empty()" will not distinguish between undefined variables and those set to false.

$isTouch = empty($variable);
Release Statement This article is reprinted at: 1729339097 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3