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

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
rorickaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SilversoftCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rorickaAuthor Commented:
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
SilversoftCommented:
$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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

SilversoftCommented:
0
rorickaAuthor Commented:
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
SilversoftCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.