How to Check for Undefined Values in PHP

Barbara Streisand
Release: 2024-10-19 19:57:02
Original
478 people have browsed it

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>
    Copy after login

    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>
    Copy after login

    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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!