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?
 
SilversoftConnect With a Mentor Commented:
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
 
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
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
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
All Courses

From novice to tech pro — start learning today.