On Fri, 2004-03-26 at 13:46, Rasmus Lerdorf wrote:
> if(!empty($variable))

This will return false positives for cases where the variable has not
been set to null but HAS been set to the empty string or to a 0? This
isn't really the same as the OP requested. However, isset() also returns
false for variables assigned null values and so he is fine with just
isset() and skipping the check for != NULL.

Contrast the following code and output:

echo ' isset: ['.isset( $foo ).']'."\n";
echo '!empty: ['.(!empty( $foo )).']'."\n";

-->  isset: []
--> !empty: []

-----------------------------------------------------

$foo = null;
echo ' isset: ['.isset( $foo ).']'."\n";
echo '!empty: ['.(!empty( $foo )).']'."\n";

-->  isset: []
--> !empty: []

-----------------------------------------------------

$foo = 0;
echo ' isset: ['.isset( $foo ).']'."\n";
echo '!empty: ['.(!empty( $foo )).']'."\n";

-->  isset: [1]
--> !empty: []

-----------------------------------------------------

$foo = 0.0;
echo ' isset: ['.isset( $foo ).']'."\n";
echo '!empty: ['.(!empty( $foo )).']'."\n";

-->  isset: [1]
--> !empty: []

-----------------------------------------------------

$foo = '';
echo ' isset: ['.isset( $foo ).']'."\n";
echo '!empty: ['.(!empty( $foo )).']'."\n";

-->  isset: [1]
--> !empty: []

-----------------------------------------------------

$foo = 'blah';
echo ' isset: ['.isset( $foo ).']'."\n";
echo '!empty: ['.(!empty( $foo )).']'."\n";

-->  isset: [1]
--> !empty: [1]

-----------------------------------------------------

Cheers,
Rob.

> 
> On Fri, 26 Mar 2004, Marcjon Louwersheimer wrote:
> 
> > 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?
> > -- 
> >   Marcjon

-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to