Link to home
Start Free TrialLog in
Avatar of thepadders
thepadders

asked on

Equivalency

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

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

Why does this match?
Avatar of nicholassolutions
nicholassolutions
Flag of United States of America image

<?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";


?>
ASKER CERTIFIED SOLUTION
Avatar of nicholassolutions
nicholassolutions
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Diablo84
Diablo84

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
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
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 ;)
>> 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
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!
Thanks neester :)
Keep up the good work yourself -- you're pretty close to a PHP cert ;)
-Matt
>> 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
Avatar of thepadders

ASKER

Thanks everyone for this, I am not sure why I didn't comment at the time. Thanks.
Glad we could help :)