Re: isset() and !=NULL

From: Date: Sun, 28 Mar 2004 09:27:39 +0000
Subject: Re: isset() and !=NULL
Groups: php.general 
Request: Send a blank email to php-general+get-181625@lists.php.net to get a copy of this message
David Risner" <drisner@fastmail.fm> wrote in message news:1080327987.25881.183392507@webmail.messagingengine.com...> On Fri, 26 Mar 2004 10:40:43 -0800, "Marcjon Louwersheimer" > <marcjon@fastmail.fm> said: > > Is there an easier way to do > > isset($variable) AND $variable != NULL > > ? I use this alot in my if statements, and I was wondering if there's an > > easier way to do it, maybe with a single function? Oh and another > > question... how does if ($variable) work? When does it evaluate true? > > Check out the is_null function. Itd seems to combine the two. > > http://www.php.net/manual/en/function.is-null.php > and > \Õàu%›alŸ4º > Jihttp://www.php.net/manual/en/language.types.null.php#language.types.null.syntax As Rasmus noted, (!empty($var)) is a complete aquivalent to (isset($var) && $var != null). I ran some more extensive tests which show the following: class MyClass { var $field = 123; function MyClass() {} } 1. $x = null; 2. $y = 0; 3. $z = ''; 4. $w = 'xyz'; 5. $t = 123; 6. $o = new MyClass(); 7. // $v is not set -------------------------------- 1. isset($x) && $x != NULL: [] 2. isset($y) && $y != NULL: [] 3. isset($z) && $z != NULL: [] 4. isset($w) && $w != NULL: [1] 5. isset($t) && $t != NULL: [1] 6. isset($o) && $o != NULL: [1] 7. isset($v) && $v != NULL: [] -------------------------------- 1. isset($x): [] 2. isset($y): [1] 3. isset($z): [1] 4. isset($w): [1] 5. isset($t): [1] 6. isset($o): [1] 7. isset($v): [] -------------------------------- 1. !empty($x): [] 2. !empty($y): [] 3. !empty($z): [] 4. !empty($w): [1] 5. !empty($t): [1] 6. !empty($o): [1] 7. !empty($v): [] -------------------------------- 1. is_null($x): [1] 2. is_null($y): [] 3. is_null($z): [] 4. is_null($w): [] 5. is_null($t): [] 6. is_null($o): [] Notice: Undefined variable: v in c:\web\test.php on line 62 7. is_null($v): [1] Also, as you see is_null($var) throws a notice when the variable is not defined. Hope this helps ;) Regards, Dimiter

« previous php.general (#181625) next »