search for in the  
<doublevalfloatval>
Last updated: Thu, 19 May 2005

empty

(PHP 3, PHP 4, PHP 5 )

empty -- Determine whether a variable is empty

Description

bool empty ( mixed var )

Determine whether a variable is considered to be empty.

Parameters

var

Variable to be checked

Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

Return Values

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

ChangeLog

VersionDescription
PHP 5

As of PHP 5, objects with no properties are no longer considered empty.

PHP 4

As of PHP 4, The string value "0" is considered empty.

Examples

Example 1. A simple empty() / isset() comparison.

<?php
$var
= 0;

// Evaluates to true because $var is empty
if (empty($var)) {
   echo
'$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
   echo
'$var is set even though it is empty';
}
?>

Notes

Note: Because this is a language construct and not a function, it cannot be called using variable functions



User Contributed Notes
empty
myfirstname dot barros at gmail dot com
29-Apr-2005 10:15
<?php
$a
= Array( ); #<- empty
$a = Array( '' ); #<- NOT empty
$a = Array( Null ); #<- NOT empty
?>

---
gabriel
rehfeld.us
23-Aug-2004 03:47
ive found the empty() contruct extremely usefull. For some reason people seem to think its of little use, but thats not so.

for example, form fields can be checked in 1 step by using empty(). (assuming a basic check of whether it was submitted and if submitted, that it was not empty.)

<?php

if (!empty($_POST['name'])) $name = $_POST['name'];

?>

compared to isSet(), this saves an extra step. using !empty() will check if the variable is not empty, and if the variable doesnt exit, no warning is generated.

with isSet(), to acheive the same result as the snippit above, you would need to do this:

<?php

if (isSet($_POST['name']) && $_POST['name']) $name = $_POST['name'];

?>

so using !empty() reduces code clutter and improves readability, which IMO, makes this VERY usefull.
paul at worldwithoutwalls dot co dot uk
22-May-2004 12:09
Note the exceptions when it comes to decimal numbers:

<?php
$a
= 0.00;
$b = '0.00';
echo (empty(
$a)? "empty": "not empty"); //result empty
echo (empty($b)? "empty": "not empty"); //result not empty
//BUT...
$c = intval($b);
echo (empty(
$c)? "empty": "not empty"); //result empty
?>

For those of you using MySQL, if you have a table with a column of decimal type, when you do a SELECT, your data will be returned as a string, so you'll need to do apply intval() before testing for empty.

e.g.
TABLE t has columns id MEDIUMINT and d DECIMAL(4,2)
and contains 1 row where id=1, d=0.00
<?php
$q
= "SELECT * FROM t";
$res = mysql_query($q);
$row = mysql_fetch_assoc($res);
echo (empty(
$row['d'])? "empty": "not empty"); //result not empty
?>
phpcheatsheet at blueshoes dot org
27-Nov-2002 01:18
the php cheatsheet gives a good overview for empty(), isSet(), is_null() etc. http://www.blueshoes.org/en/developer/php_cheat_sheet/

to chris at chaska dot com:
that line
if ( ! isset( $var ) ) return TRUE;
won't do anything, it's useless in that scope.

<doublevalfloatval>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT