Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP Difference between empty and = ""

LEGEND ,
Nov 02, 2006 Nov 02, 2006
Is there any major difference between empty and = ""? Perfomance, instances
where one is better over the other?

--

TIA,

Jon Parkhurst
PriivaWeb
http://priiva.net.


TOPICS
Server side applications
508
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
crash wrote:
> Is there any major difference between empty and = ""?

The empty() function checks whether a variable is empty, as defined here:

http://www.php.net/manual/en/function.empty.php

= "" sets a variable to an empty string.

Do you mean the difference between using empty() and == ""?

If so, there is a major difference.

$variable = 0;

if (empty($variable)) // true
if ($variable == "") // false

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
Sorry David, I should have rephrased.
Is there any notable difference between using an existing PHP function to
determine empty Versus using PHP function to compare strings to empty values

$a = "";

if(!empty($a)){
print $a;
}

and this

if($a !=""){
$print $a;
}

Are there conditions one would catch and another would not? For example, in
your code:

$a=0;

empty will see that as true (or empty), correct? Are there any other
conditions where empty would not verify.

The reason I'm asking is I'm putting in error checking in a lot of our
sites, and wish to know which I should be using.

I prefer empty(), as I think it looks cleaner, (and I think 0 values being
empty would be fine, as it would be an invalid record call), but want to
make sure I don't need to use != "".


"David Powers" <david@example.com> wrote in message
news:eid503$25r$1@forums.macromedia.com...
> crash wrote:
>> Is there any major difference between empty and = ""?
>
> The empty() function checks whether a variable is empty, as defined here:
>
> http://www.php.net/manual/en/function.empty.php
>
> = "" sets a variable to an empty string.
>
> Do you mean the difference between using empty() and == ""?
>
> If so, there is a major difference.
>
> $variable = 0;
>
> if (empty($variable)) // true
> if ($variable == "") // false
>
> --
> David Powers
> Adobe Community Expert
> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
> http://foundationphp.com/


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
crash wrote:
> Are there conditions one would catch and another would not? For example, in
> your code:
>
> $a=0;
>
> empty will see that as true (or empty), correct? Are there any other
> conditions where empty would not verify.

Yes. That's why I pointed you to the online documentation. PHP lays down
exact rules about what is considered empty. Gareth may join in at some
stage, because I know he's had problems with either empty() or isset().

isset() checks whether a variable has been defined.
empty() checks whether the value of the variable meets the criteria of
"emptiness".

Another example:

$var = array();

In this case, $var has been defined, but it's empty. So, both isset()
and empty() would return true.

You need to be very precise about what you're checking. For instance,
you might think the following is empty:

$var = ' ';

It's not. Although a human regards a blank space as empty, a space
character doesn't fit the criteria laid down in the PHP definition of
what equates to empty.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
> Yes. That's why I pointed you to the online documentation. PHP lays down
> exact rules about what is considered empty. Gareth may join in at some
> stage, because I know he's had problems with either empty() or isset().

I read the PHP site, just wanted to clarify, I appreciate your input very
much!

If I remember correctly, Gareth was isset. (if thread was month 1/2 ago)


> isset() checks whether a variable has been defined.
> empty() checks whether the value of the variable meets the criteria of
> "emptiness".
>
> Another example:
>
> $var = array();
>
> In this case, $var has been defined, but it's empty. So, both isset() and
> empty() would return true.

Not a problem. I don't use isset any longer unless I"m checking to see if
another condition has been met.


> You need to be very precise about what you're checking. For instance, you
> might think the following is empty:
>
> $var = ' ';
>
> It's not. Although a human regards a blank space as empty, a space
> character doesn't fit the criteria laid down in the PHP definition of what
> equates to empty.

Agreed. And both if($var !="") and if (!empty($var)) would execute in the
above code. I believe I understand and should be fine using empty().

Thanks for your help David. PHP.net is not always the most "example
friendly" - ie, I can't always visualize my implementations with their
examples. The code posted there is a bit more complicated than what I'm
tyipcally using.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
.oO(crash)

>Agreed. And both if($var !="") and if (!empty($var)) would execute in the
>above code. I believe I understand and should be fine using empty().

One thing to keep in mind: empty() requires its parameter to be a
variable, not an expression. Something like

if (empty(getSomeString()) {
...
}

won't work.

Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
I'm inside all my functions now, so I should be cool.

Thanks for the note, though!

"Michael Fesser" <netizen@gmx.de> wrote in message
news:0snkk21lbjv4ko93ci8e8n6kniu3p7rajj@4ax.com...
> .oO(crash)
>
>>Agreed. And both if($var !="") and if (!empty($var)) would execute in the
>>above code. I believe I understand and should be fine using empty().
>
> One thing to keep in mind: empty() requires its parameter to be a
> variable, not an expression. Something like
>
> if (empty(getSomeString()) {
> ...
> }
>
> won't work.
>
> Micha


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
On Thu 02 Nov 2006 04:25:23p, crash wrote in macromedia.dreamweaver.appdev:

> I'm inside all my functions now

Is this the start of some new Zen-like kind of web development?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 02, 2006 Nov 02, 2006
.oO(Joe Makowiec)

>On Thu 02 Nov 2006 04:25:23p, crash wrote in macromedia.dreamweaver.appdev:
>
>> I'm inside all my functions now
>
>Is this the start of some new Zen-like kind of web development?

Some people even sleep() with their scripts ...

SCNR
Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 03, 2006 Nov 03, 2006
I find that if I don't everything gets in dis array - and that makes me want
to explode....

<groooooooan>

>>> I'm inside all my functions now
>>
>>Is this the start of some new Zen-like kind of web development?
>
> Some people even sleep() with their scripts ...
>
> SCNR
> Micha


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 03, 2006 Nov 03, 2006
.oO(crash)

>I find that if I don't everything gets in dis array - and that makes me want
>to explode....
>
><groooooooan>

Unix for adults:

who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount;
gasp; yes; uptime; umount; sleep

Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 03, 2006 Nov 03, 2006
LATEST
ROFL nice, i bow to the master. ;o)


"Michael Fesser" <netizen@gmx.de> wrote in message
news:carmk2lf35ri8moqjsuj1nas6t9ofinm7j@4ax.com...
> .oO(crash)
>
>>I find that if I don't everything gets in dis array - and that makes me
>>want
>>to explode....
>>
>><groooooooan>
>
> Unix for adults:
>
> who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount;
> gasp; yes; uptime; umount; sleep
>
> Micha


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines