RE: [PHP] empty and isset
| From: | Ford, Mike [LSS] | Date: | Fri, 07 Feb 2003 13:45:56 +0000 |
| Subject: | RE: [PHP] empty and isset | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-134717@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Sunfire [mailto:rusty_81@msn.com]
> Sent: 06 February 2003 21:27
>
> actually so does empty end up testing true on an empty var...
> thats because
> empty thinks "" or string(0) is actually a string just
> blank... a trick to
> do with empty is use !empty which will say if the string is
> at least 1 char
> long other wish if it is less than 1 char long even though a
> string does
> exist then it must be empty.. because:
> if(empty(var)){
> echo "its blank";//eek doesnt work cuz string does exist
> }else{
> echo "something in there";//defaults regardless string(0)""
> //is a string so above block will never be used
> //im sort of confused because:
> if(!empty(var)){
> echo "something in there";//it works for a wierd reason
> }else{
> echo "its blank"//works if string(0)"" or null exists
> }
> its strange that !empty will always return a 0 char
> string/null as false but
> empty it doesnt care its always true regardless... any reason
> for that? cuz
> im confused as to why you have to use !empty instead of empty
OK, let's set some of this confusion to rest:
isset($x) will be TRUE if $x *both*:
- exists as a variable in the current scope (even if it
has not yet been assigned a value, as might happen if you
use a var or global declaration).
- is not NULL
empty($x) will be TRUE if $x is *any* of the following:
- a non-existent variable (in the current scope)
- NULL
- a numeric 0 (or 0.0)
- the empty string ("") or the string "0"
- an empty array
is_null($x) is the same as !isset($x), except that is_null()
produces an undefined variable notice if $x does not exist
(which you will not see if your error_reporting is set not to
display notices, or if you suppress it with @).
and finally, if($x) is the same as if(!empty($x)), except
that if($x) will also produce an undefined variable notice.
This is all defined in the online manual, although it's
sometimes a bit hard to digest. There's also a great little
set of tables that may help you visualize things better at
http://www.blueshoes.org/en/developer/php_cheat_sheet/
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211