Re: confused: casting a variable to integer



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
.



Relevant Pages

  • Re: findfile?
    ... write some simple loop which will go through directories and compare ... then compare values recursively in that array ... array scandir (string directory ... Availability: PHP 5 CVS only ...
    (comp.lang.php)
  • Re: String Identity Test
    ... I will make a PHP example ... difference in Python. ... in case of a string means the interpreter has to find out about ... If you only have a few strings but compare them often, ...
    (comp.lang.python)
  • Re: Odd String Comparison Problem
    ... issue down to a simple string comparison problem. ... string with the explode function of PHP. ... The problem is that under no circumstance and with no compare method ...
    (comp.lang.php)
  • Re: MD5 implementation
    ... How do I get a string in the .NET version that I can compare to the PHP ... >> Casper Hornstrup ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Object of class Person could not be converted to string
    ... PHP has been lousy at maintaining backward compatibility. ... Objects are always converted to the string "Object". ... there's no reasonable gain in usability by forcing it ...
    (comp.lang.php)