note 90767 added to function.empty
| From: | emperoruk at dontspam dot hotmail dot com | Date: | Fri, 08 May 2009 15:07:06 +0000 |
| Subject: | note 90767 added to function.empty | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154365@lists.php.net to get a copy of this message | ||
When using the php empty() function to check submitted variables such as $_POST or $_GET, be careful
to remember that values 0 (integer) and "0" (string with zero character) are all
considered empty. eg. in a simple cms a page ID of zero might be used to indicate that the homepage
should be displayed but using the following code:
<?php
if (isset($_GET['pid'] && !empty($_GET['pid']) {
// assign value to local variable
$pageID = $_GET['pid'];
} else {
echo "missing variable 'pageID'";
}
?>
When attempting to display the homepage using a pid of zero the above code will fail.
So as a result i wrote a small function to replace the php empty() function in situations where you
want 0 and "0" not to be considered empty.
<?php
function is_empty($var, $allow_false = false, $allow_ws = false) {
if (!isset($var) || is_null($var) || ($allow_ws == false && trim($var) == ""
&& !is_bool($var)) || ($allow_false === false && is_bool($var) && $var ===
false) || (is_array($var) && empty($var))) {
return true;
} else {
return false;
}
}
?>
This function will allow you to test a variable is empty and considers the following values as
empty:
an unset variable -> empty
null -> empty
0 -> NOT empty
"0" -> NOT empty
false -> empty
true -> NOT empty
'string value' -> NOT empty
" " (white space) -> empty
array() (empty array) -> empty
There are two optional parameters:
$allow_false: setting this to true will make the function consider a boolean value of false as NOT
empty. This parameter is false by default.
$allow_ws: setting this to true will make the function consider a string with nothing but white
space as NOT empty. This parameter is false by default.
In Testing:
<?php
// an unset variable
echo 'unset variable ($notset) - Empty: ';
echo is_empty($notset) ? 'yes<br />' : 'no<br />';
// NULL variable
echo 'null - Empty: ';
$var = null;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// integer 0
echo '0 - Empty: ';
$var = 0;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// string "0"
echo 'string "0" - Empty: ';
$var = "0";
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// boolean value false
echo 'false - Empty: ';
$var = false;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// allow boolean value false
echo 'false ($allow_false = true) - Empty: ';
$var = false;
echo is_empty($var, true) ? 'yes<br />' : 'no<br />';
// boolean value true
echo 'true - Empty: ';
$var = true;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// string
echo 'string "foo" - Empty: ';
$var = "foo";
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// white space
echo 'white space " " - Empty: ';
$var = " ";
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// allow white space
echo 'white space ($allow_ws = true) " " - Empty: ';
$var = " ";
echo is_empty($var, false, true) ? 'yes<br />' : 'no<br />';
// empty array
echo 'empty array - Empty: ';
$var = array();
echo is_empty($var) ? 'yes<br />' : 'no<br />';
?>
the above code outputs the following:
unset variable ($notset) - Empty: yes
null - Empty: yes
0 - Empty: no
string "0" - Empty: no
false - Empty: yes
false ($allow_false = true) - Empty: no
true - Empty: no
string "foo" - Empty: no
white space " " - Empty: yes
white space ($allow_ws = true) " " - Empty: no
empty array - Empty: yes
Hope this code is useful for someone.
Michael
----
Server IP: 92.48.74.199
Probable Submitter: 86.7.12.43
----
Manual Page -- http://www.php.net/manual/en/function.empty.php
Edit -- https://master.php.net/note/edit/90767
Del: integrated -- https://master.php.net/note/delete/90767/integrated
Del: useless -- https://master.php.net/note/delete/90767/useless
Del: bad code -- https://master.php.net/note/delete/90767/bad+code
Del: spam -- https://master.php.net/note/delete/90767/spam
Del: non-english -- https://master.php.net/note/delete/90767/non-english
Del: in docs -- https://master.php.net/note/delete/90767/in+docs
Del: other reasons-- https://master.php.net/note/delete/90767
Reject -- https://master.php.net/note/reject/90767
Search -- https://master.php.net/manage/user-notes.php