Re: Beginners query
| From: | Ashley Sheridan | Date: | Thu, 01 Jan 2015 08:53:27 +0000 |
| Subject: | Re: Beginners query | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324203@lists.php.net to get a copy of this message | ||
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