You are here: Re: [PHP] if statement probable bug « PHP « IT news, forums, messages
Re: [PHP] if statement probable bug

Posted by Jochem Maas on 03/24/05 16:55

TheI2eptile wrote:
> Probably this is the wrong place to put this but I couldn't search for

not at all the wrong place, having said that the only thing probable with regard
to you/your post is that you are not fully aware of the nature of data types in
php and the way auto-typecasting works (in terms of precedence and conversion)...
which is not surprising, most people get bitten by this at some time :-) because
often when looking at the behaviour from the point of view of other
langs (or as a complete beginner) the logic behind it may not seem at all logical...

I recommend searching for some more info on (auto)typecasting in php,
by the sounds of things you're capable of grokking the whys and wherefores without
much trouble :-)

> the bug and what I saw in the bugs newsgroup was a bit strange and seem
> to be a log for the bugs forms.
>
> So here is what I would call a bug, but maybe it's thought to be so:
> <?php
> $var1 = 0;
> $var2 = "AS";
>
> if($var1 == "AS"){
> echo "<br>Why is this printed";
> }

int has higher precendence... therefore "AS" is converted to an int
.... "AS" when converted to an int is zero... the order of the operands does not
matter in this case, try the following one liner:

<?php var_dump( (0 == "AS"), ("AS" == 0) ); ?>

>
> if(0 == "AS"){
> echo "<br>At least this should not be printed";
> }

same rules applies here.

>
> if($var2 == "AS"){
> echo "<br>Only this should";
> }
> ?>
> I didn't expect it. I use php 5.02 and have the zend debugger installed.
> Does anybody see every echo and finds this strange in a typeless language?

php is not typeless! its dynamically typed.
I wonder if there are typeless langs at all :-)

btw: yes I see every echo, no I don't find it strange (anymore!)

as someone else already mentioned: use the strict equality operator (===) to
tell php not to do auto-typecasting on the vars you want to check for equality.

rgds,
Jochem

>

 

Navigation:

[Reply to this 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

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