Re: confused: casting a variable to integer
- From: Justin Koivisto <justin@xxxxxxxxx>
- Date: Tue, 29 Nov 2005 11:46:36 -0600
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@xxxxxxxxx
http://koivi.com
.
- References:
- confused: casting a variable to integer
- From: Erwin Moller
- confused: casting a variable to integer
- Prev by Date: Re: include_once/__autoload/namespace emulation ...
- Next by Date: Re: Sort array from a Query
- Previous by thread: confused: casting a variable to integer
- Next by thread: Re: confused: casting a variable to integer
- Index(es):
Relevant Pages
|