Re: Beginners query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2015-01-01 at 13:58 +0530, Sachin Raut wrote:
> Happy New Year to all members of PHP group.
> 
> I just want to know the difference between following 3 statements. Or are
> they all does the same thing.
> 
> 1. isset($a)
> 2. !empty($a)
> 3. $a!=""
> 
> Thanks
> Sachin

Happy new year to yourself as well!

Each of these is similar, but subtly different:

1. isset() checks to see if a variable exists, and doesn't care if it
has a value or not.

2. empty() checks to see if both the variable exists and the value
doesn't equate to the boolean false, an empty string, the number/string
0, null, or an empty array.

3. $a != "" just checks to see if $a equates to an empty string, note
though that PHP has loose typing, so if $a isn't a string, it's
converted to one internally just for the purposes of the comparison. If
you have to specifically check for the empty string, then !== might be
better.


There is one more that you didn't mention, which is is_null(), and that
one checks to see if a variable contains a null value, which is covered
by empty() but is a bit more specific (you may want a variable to be a
0, but you want to check it's not null before you try and use it, for
example)

Hope this helps.

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk




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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux