Reply to Re: [PHP-WIN] Is this a PHP bug?

Your name:

Reply:


Posted by tg-php on 10/14/01 11:29

It's because PHP is trying to convert different var types into a common var type and then compare them when you use ==. === means compare value AND type so that's why it fails.

Your string, 'Some kind of string' is getting converted down to the lowest common denominator, an int. Since there are no numbers in the string to grab onto, it gets converted to nothing, an int value of zero.

If you had $x = "0" or if $y had contained numbers at all, it wouldn't have passed.

But this is why when you use $x.'' it works properly because now you have "0<empty string>", it's been converted to a string value "0". Good catch on that though, shows good methodical debugging :)

So in the future, either use === or be extra aware of your variable types and make sure you're comparing properly.

Good luck!

-TG

= = = Original message = = =

$x = 0; // Numeric zero
$y = 'Some kind of string';

if ($x == $y) echo 'they equal using ==';
if ($x === $y) echo 'they equal using ===';

The above will echo 'they equal using =='.

The values don't look very equal to me.

Can anyone explain the logic behind this?

I'm heading home now but look forward to your explanations tomorrow.

PS

Incidently, to 'fix' it so it behaves as it should, you can code:

if ($x.'' == $y.'') echo 'this will not print and all is good.';

Regards .. Ross


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация