Reply to Re: confused: casting a variable to integer

Your name:

Reply:


Posted by Justin Koivisto on 11/29/05 19:46

Erwin Moller wrote:
> Hi group,
>
> Maybe I should stop working because this seems soo basic.
> I almost feel ashamed to ask, but here we go.
> :-/
>
> Consider the following script:
>
> <?
> $name="henk";
>
> echo "\$name=$name <br>";
> echo "(int)\$name=".(int)$name."<br>";
>
> if ($name == (int)$name){
> echo "equal";
> } else {
> echo "Not equal";
> }
> ?>
>
> produces:
> -----------------
> $name=henk
> (int)$name=0
> equal
> ------------------
>
> What am I missing here?
>
> Is PHP casting the string "henk" to 0 somehow?

Yes... When evaluating an expression, PHP uses the lowest common type.
int is simpler than string, therefore, to compare them for equality, PHP
must make them both the same type and then compare.

Once I can find time to write some more articles for my new site. "Type
Juggling" is actually the next on this list. ;) (URL withheld from
public post. If you are curious/interested in the site, email me.)

HTH

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.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

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