Home >Backend Development >PHP Tutorial >How to Check for Undefined Values in PHP

How to Check for Undefined Values in PHP

Barbara Streisand
Barbara StreisandOriginal
2024-10-19 19:57:02587browse

How to Check for Undefined Values in PHP

Determining Undefined Values in PHP

In PHP, unlike JavaScript, the isset() function is not solely used to check if a variable is undefined. It verifies whether a variable is declared and not explicitly set to NULL. To address the question of checking for undefined values, consider the following solutions:

  • isset() Function:

    <code class="php">$isTouch = isset($variable);</code>

    This will return true if the variable is defined, and false if it's undefined or set to NULL.

  • Empty() Function:

    <code class="php">$isTouch = empty($variable);</code>

    empty() further verifies that a variable is undefined or contains false, 0, 0.0, "0", NULL, an empty array, or a declared variable without a value.

Note:

  • isset() returns true if the variable exists and has a value other than NULL.
  • empty() works for the following values:

    • "" (empty string)
    • 0 (integer)
    • 0.0 (float)
    • "0" (string)
    • NULL
    • FALSE
    • array() (empty array)
    • $var; (variable declared without a value)

The above is the detailed content of How to Check for Undefined Values 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