Equivalency

$a = 23;
$b = '23x';

if ($a == $b) {
      echo "matched";
}

Why does this match?
thepaddersAsked:
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.

nicholassolutionsCommented:
<?php
$a = 23;
$b = '23x';

if ($a == $b) {
     echo "matched";
}

if ("$a" == "$b") {
     echo "matched";
}
else echo "didn't match";

if ($a === $b) {
     echo "matched";
}
else echo "didn't match";


?>
nicholassolutionsCommented:
whoops, sorry I didn't mean to post that just yet. Here's the explanation:
Have a look here: http://docs.php.net/en/language.operators.html#language.operators.comparison

In the first comparison, you are comparing whether the two values are equal. $a is a numerical value, and $b is a string. In most languages, this would give you an error. PHP is forgiving in this respect, and trys its best to figure out what you mean, so it says 'by 23x I'm guessing you mean 23' and it compares them and you get a math. In the second comparison, you are matching two strings, because I enclosed both in quotes (in fact, doing "$a" == $b would be the same). Obviously they don't match. The last match checks not only if the two are equal, but if they are the *same data type* which they aren't, so again it doesn't match.

hope that helps,
Matt

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
nicholassolutionsCommented:
This also will help explain it: http://docs.php.net/en/function.settype.html

Notice how settype($foo, "integer") where $foo="5bar" changes $foo to 5.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Diablo84Commented:
Just to expand on the subject of why it happens...

If you compare a string to a number, the string is actually converted to a number. This number will either be interpreted as a float or an integer (if . or e is present in the string it will be a float, otherwise it will be an int). If the string begins with numeric data then this will be used otherwise the value will be 0. This numeric data is a sign (+/- optional) followed by at least one digit. Anything that follows will be disregarded.

Diablo84
Diablo84Commented:
Better explained in the manual... after i found the page i was looking for... here: http://us2.php.net/manual/en/language.types.string.php#language.types.string.conversion

Probably would have been easier if i had noticed that the page was linked to within the first link that Matt posted before i went searching :)

Diablo84
nicholassolutionsCommented:
Thanks Diablo84 -- that's much clearer. Here's another page that's helpful in learning this stuff:
http://docs.php.net/en/language.types.html#language.types.type-juggling

PHP's type juggling abilities are usually very useful and convenient, and most people (especially beginners) don't bother to learn about data types carefully (most introductory books don't talk much about it), but getting a handle on it really isn't that hard, and it's worth the time so that you don't get burned later when you can't figure out what's wrong with a piece of code.

Good luck :)
Matt
nicholassolutionsCommented:
LOL -- cross posted with you there. Too bad we can't make this a sticky or something, I think we've found almost every page in the docs that talks about data types ;)
Diablo84Commented:
>> Too bad we can't make this a sticky or something

Hopefully in the future we will see a "best of" feature which will allow the best content to be kept separate from the PAQ.

I am thinking of opening a PAQ/FAQ thread in the mean time (briefly mentioned in the discussion thread) which will include answers to commonly asked questions as well as pieces of information and other such notes. It would certainly be a useful resource.

Diablo84
neesterCommented:
Very good answers guys!
I was thinking of things to post, but as I read every answer had 10* what I would have written!

pro answers here!
a definite split here!

:D

wd Matt and Diablo!
nicholassolutionsCommented:
Thanks neester :)
Keep up the good work yourself -- you're pretty close to a PHP cert ;)
-Matt
neesterCommented:
>> you're pretty close to a PHP cert ;)

yeah getting there!
its hard now that my main job isn't web dev. related anymore.
still - its a passion of mine.
:D

thnx
thepaddersAuthor Commented:
Thanks everyone for this, I am not sure why I didn't comment at the time. Thanks.
nicholassolutionsCommented:
Glad we could help :)
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.