For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > November 2005 > confused: casting a variable to integer









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author confused: casting a variable to integer
Erwin Moller

2005-11-29, 6:57 pm

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?

Regards,
Erwin Moller
Justin Koivisto

2005-11-29, 6:57 pm

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
Sean

2005-11-29, 6:57 pm

See the differece if you use ===

<?
$name="henk";

echo "\$name=$name <br>";
echo "(int)\$name=".(int)$name."<br>";

if ($name === (int)$name){
echo "equal";
} else {
echo "Not equal";
}

?>

http://php.net/operators.comparison

Justin Koivisto

2005-11-29, 6:57 pm

Sean wrote:
> See the differece if you use ===
>
> <?
> $name="henk";
>
> echo "\$name=$name <br>";
> echo "(int)\$name=".(int)$name."<br>";
>
> if ($name === (int)$name){
> echo "equal";
> } else {
> echo "Not equal";
> }
>
> ?>
>
> http://php.net/operators.comparison
>


But be careful with this...

$i="4";
if($i === intval($i)){
// this will not happen
}

Also keep in mind that values that are supplied via _GET, _POST, _COOKIE
are strings, not their representative types...

Therefore:

call page: page.php?i=4
if($_GET['i'] === intval($_GET['i'])){
// this will not happen
}

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com