tags:

views:

45

answers:

3

Hello. How can I cast to integer in PHP using only 1 or two chars?

If I have the operation $a = (int) $b; it will result using those two (or one) chars: $a = <insert the needed 1 or 2 chars> $b;

I need only to cast to integer. Thank you.

A: 
$a = sprintf("%02d", $b);

If you need to pad it with something other than zero, replace the zero with the character you need.

Igor Zinov'yev
A: 

You can make a function, I guess. Of other type of shorter cast I'm unaware.

function z($i)
{
return (int)$i;
}

$a = z($b);
Bogdan Constantinescu
+2  A: 

Prefix a + (the unary plus):

$a=+$b

A pretty common trick for code-golfing that also works in PowerShell and other languages.

$ php -r "var_dump('4');"
string(1) "4"
$ php -r "var_dump(+'4');"
int(4)
Joey
I knew there would be something like this... +1
alex