Solved

In PHP, how can "==" distinguish between 0 and an empty string?

Posted on 2004-11-02
260 Views
Last Modified: 2010-08-05
In PHP, if I code:

if (0 == "")
  echo("zero the same as empty string");
else
  echo("zero different from empty string");

I get "zero the same as empty string".

Similarly, if I code:
$x = 0;
$y = "";
if ($x == $y)
  echo("zero the same as empty string");
else
  echo("zero different from empty string");

Does this mean, short of invoking something such as strlen(),  there is NO WAY to distinguish between the integer 0 and the empty string "" in php?? Or to put it another way, if I only use the "==" operator, is there any way to tell the difference between "" and 0 in php?

roricka
0
Question by:roricka
    6 Comments
     
    LVL 2

    Accepted Solution

    by:
    Hi

    Try use "===" instead of "==" for comparisons like this:

    <?
    $x = 0;
    $y = "";
    if ($x === $y)
      echo("zero the same as empty string");
    else
      echo("zero different from empty string");
      ?>
    0
     

    Author Comment

    by:roricka
    Wow, experts exchange to the rescue! THanks Silversoft.
    (Well, technically you didn't answer my question (I asked about ==) but clearly you gave me what I needed!)

    BTW, what is another situation where the === operator comes in handy, or was it specially invented to handle this one case??

    Thanks again.

    roricka
    0
     
    LVL 2

    Expert Comment

    by:Silversoft
    $a == $b   >> Equal TRUE if $a is equal to $b.
    $a === $b >> Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)  
    0
     
    LVL 2

    Expert Comment

    by:Silversoft
    0
     

    Author Comment

    by:roricka
    That link to the manual isn't very enlightening.

    Are you saying

    5=="5" is true but 5==="5" is false?

    Are there other cases besides numbers compared to strings where "===" comes in handy?
    0
     
    LVL 2

    Expert Comment

    by:Silversoft
    I think it only come handy when the var type make a difference in the comparison.
    I hardly use the === operator, not too sure where else it becomes handy beside the var type....
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Suggested Solutions

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now