emptyDescriptionempty() 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. See alsoTo compare one value to another: ==, ===, !=, !==, <, >, <=, >= operators To test whether a variable is set: ExampleExample 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.
|