PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




empty

Name

empty — Tests whether a variable is defined and contains a non-empty and nonzero value.

Synopsis

bool empty(variable);
mixed variable: Variable to test

Returns

TRUE if the variable is empty or undefined; FALSE otherwise

Description

empty() checks whether the argument passed is a defined variable that contains a value other than an empty array, empty string, 0 (zero), or NULL. empty() should be used only on variables - if the function is used on a value, it generates a parse error.

Version

PHP Version: 3+, 4+

See also

To compare one value to another:

==, ===, !=, !==, <, >, <=, >= operators

To test whether a variable is set:

isset()

Example

Example 1377. Test whether a single value is empty

$value = '';

if (empty ($value))
    print "Variable <i>\$value</i> is empty.\n\n";
else
    print "Variable <i>\$value</i> is not empty.\n\n";


// Alternate syntax for the same test
// Because of the if statement, $value is evaluated in a true/false context
// .. basically meaning that non-empty and non-zero means true.
if ($value)
    print "Variable <i>\$value</i> is empty.\n\n";
else
    print "Variable <i>\$value</i> is not empty.\n\n";


// Another alternate syntax for the same test using the == operator
if ($value == 0)
    print "Variable <i>\$value</i> is empty.\n\n";
else
    print "Variable <i>\$value</i> is not empty.\n\n";


// Test whether a $value actually contains an empty string or 0 using the === operator
if ($value === '')
    print "Variable <i>\$value</i> contains an empty string.\n\n";
else if ($value === array ())
    print "Variable <i>\$value</i> contains an empty array.\n\n";
else if ($value === 0)
    print "Variable <i>\$value</i> contains 0.\n\n";
else if ($value === NULL)
    print "Variable <i>\$value</i> contains NULL.\n\n";
else
    print "Variable <i>\$value</i> does not contain an empty string or a 0.\n\n";

Example 1378. Make sure that a value contains something other than an empty string or 0

<pre>
<?
// Make a list of values to test
$values = array(
    'An empty string ("")' => "", 0 => 0, 1 => 1, 'foo' => 'foo',
    'A single space (" ")' => " ", "'000'" => '000', '000' => 000
);

// Loop through the values
// Show what they look like before and after conversion
foreach ($values as $key => $value) {
    $is_empty = empty ($value) ? 'empty' : 'not empty';
    printf ("<b>%-24s</b> %s\n", $key, "Value is $is_empty");
}
?>
</pre>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.