note 31709 added to function.empty

From: Date: Thu, 01 May 2003 01:25:35 +0000
Subject: note 31709 added to function.empty
Groups: php.notes 
Request: Send a blank email to php-notes+get-47312@lists.php.net to get a copy of this message
I agree that this function is not useful. There is a fundamental difference between an empty set and the value 0. I use the function def() for testing, wether a variable have an value or not: function def ($var) { if ("$var" != "") { return(1); } else { return(0); }; }; It is very important, to write '"$var" != ""', otherwise this function will have the same behavior than empty()! Of course, def() will not work with an array! If I know, that a variable is defined as an array, I simply use the function count. I'll never use a variable without initialisation. If I need a new array or some variable, I simply type $foo = ""; $foo_array = array(); Of course, there is a little risk in this and is_array is a very useful function! You can also define def() more general: function def ($var) { if (is_array($var)) { return(count($var)); } elseif ("$var" != "") { return(1); } else { return(0); }; }; -- http://www.php.net/manual/en/function.empty.php http://master.php.net/manage/user-notes.php?action=edit+31709 http://master.php.net/manage/user-notes.php?action=delete+31709 http://master.php.net/manage/user-notes.php?action=reject+31709

« previous php.notes (#47312) next »