[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-11-02
6
Medium Priority
?
277 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
Comment
Question by:roricka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
Silversoft earned 300 total points
ID: 12472555
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
ID: 12472786
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
ID: 12472949
$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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:Silversoft
ID: 12472961
0
 

Author Comment

by:roricka
ID: 12480456
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
ID: 12480506
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

656 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