Posted by Michael Fesser on 12/04/06 17:28
..oO(Promextheus Xex)
>I'm having a problem with numbers. I'm getting a number from an array that
>seems to have a padded space at the beginning.
>
>IE: " 5"
>
>What I've done was $var = ltrim($var) which removes the space but with one
>problem. Now I think PHP thinks it's a sting and not a number.
>
>Then I try $var = settype($var, "integer") to see if it will become a
>numeric for addition purposes.
Usually you don't have to explictly set a type. Just use the variable
as-is (even if there's leading white space, it shouldn't matter), PHP
will automatically convert it when necessary.
$var = ' 5';
print $var+10;
outputs 15. If you still want to do an explicit typecast, just write
$var = (int)$var;
That's enough. There's need for ltrim() and settype() in this case.
>Next problem... $var is now 0 and gettype($var) shows its and integer.
>
>Why would it be 0 and not 5? Perhaps there's an obvious starring right at
>me and I'm not seeing it?
There's definitely something wrong, if you get a 0. What version of PHP
do you use?
Micha
[Back to original message]
|