YouTip LogoYouTip

Php Empty Function

PHP empty() Function

PHP empty() Function

-- Learning more than just technology, but dreams!

PHP Tutorial

PHP Forms

PHP Advanced Tutorial

PHP 7 New Features

PHP Database

PHP XML

PHP and AJAX

PHP Reference Manual

PHP Regular Expressions (PCRE)

PHP Dependency Management Tool

Deep Dive

  • Web Design and Development
  • Scripting Languages
  • Development Tools
  • Software
  • Scripts
  • Web Service
  • Programming Languages
  • Web Services
  • Programming
  • Computer Science

PHP empty() Function

PHP Available Functions PHP Available Functions

The empty() function is used to check whether a variable is empty.

empty() determines whether a variable is considered empty. A variable is considered empty if it does not exist or its value is equivalent to FALSE. If the variable does not exist, empty() does not generate a warning.

Since PHP 5.5, empty() supports expressions, not just variables.

Version requirement: PHP 4, PHP 5, PHP 7

Syntax

bool empty ( mixed $var )

Parameter explanation:

  • $var: The variable to be checked.

Note: Before PHP 5.5, empty() only supported variables; anything else would cause a parse error. In other words, the following code would not work:

empty(trim($name))

As an alternative, you should use:

trim($name) == false

empty() does not generate a warning, even if the variable does not exist. This means empty() is essentially equivalent to !isset($var) || $var == false.

Return Value

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise, returns TRUE.

The following variables are considered empty:

  • "" (empty string)
  • 0 (integer 0)
  • 0.0 (float 0)
  • "0" (string "0")
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable that is declared but has no value)

Example

Example

<?php
$ivar1 = 0;
$istr1 = 'Tutorial';

if (empty($ivar1)) {
    echo '$ivar1' . " is empty or 0." . PHP_EOL;
} else {
    echo '$ivar1' . " is not empty or not 0." . PHP_EOL;
}

if (empty($istr1)) {
    echo '$istr1' . " is empty or 0." . PHP_EOL;
} else {
    echo '$istr1' . " string is not empty or not 0." . PHP_EOL;
}
?>

The execution result is as follows:

$ivar1 is empty or 0.
$istr1 string is not empty or not 0.

PHP Available Functions PHP Available Functions

← Php Get_Resource_Type FunctionPhp Debug_Zval_Dump Function β†’