|
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]
|