hey
is there a function for removing a leading 0 from expample 09 or 03 so they will become plain 9 and 3
hey
is there a function for removing a leading 0 from expample 09 or 03 so they will become plain 9 and 3
type casting.... example echo(int) $var;
$var= 09;
$var2 = 09145.76635;
echo number_format($var)."<br>"; //outputs 9
echo number_format($var2,2); //outputs 9,145.77
Houdini's example has a subtle bug, but that's because the example uses octal representation for integers instead of dealing with numeric strings in decimal representation.
<?php
$var = 010;
echo number_format($var); //outputs 8
?>
I'd say typecasting is what you want here, unless you really need the extra functionality offered by number format:
<?php
$var = "010"; //numeric string!
echo (int)$var; //outputs 10
?>
<?php
$var = "01";
if($var[0] == 0)
{
$var = $var[1];
echo $var;
}
?>
Typecast of an int will do the trick. But just for shits and giggles I decided to write out how else you could do it.
btw laser, you don't need it to be in a string for typecast to work. It will work without it being a string.
Chad
And if you want to leave it as a sting but loose the leading zero then ltrin it
$var = '09';
$var2 = ltrim($var1, '0');
echo $var2; // outputs 9
btw laser, you don't need it to be in a string for typecast to work. It will work without it being a string.
It will work, but it may not work as expected. Read my code snippets.
And if you want to leave it as a sting but loose the leading zero then ltrin it
The problem with that comes when the numeric string only contains zeros. It will be trimmed to an empty string, which may not be what the OP wants.
Oh, well, if we're playing around with different ways of casting a string to a number...
$var='09';
echo 0+$var;
Why not use explode() ?
if ($num<10)
$num=explode('0',$num);
:evilgrin:
Why not use explode() ?
Because (in the general case for non-negative integers, not just for non-negative integers less than 10) you have to check that the resulting array has more than 1 element, then array_shift() and implode the array that's left. Pretty complex for a simple job, if you ask me.
Oh, but even that may not work (with just one pass), if the original input has more than one leading zero.
chads2k2 wrote:btw laser, you don't need it to be in a string for typecast to work. It will work without it being a string.
No it won't. At least not in PHP 5.1. If you create a var as an number starting with 0, it is an octal, and if it isn't valid, at least PHP 5.1 converts it to int(0). 09 isn't a valid octal... octals are base 8 and contain only digits 0-7.
And even then if you have a valid octal, it is converted to the base 10 equivalent, which is only the "same" as the octal symbol for 0-7, as laserlight pointed out because 10 (base8) = 8 (base10).
PowerBook-G4:~ dreamscape$ php -a
Interactive mode enabled
php > $var = 09;
php > echo number_format($var);
php > echo (int)$var;
php > echo (float)$var;
php > echo (string)$var;
php > // what is var?
php > var_dump($var);
int(0)
php >
I think as laserlight suggested, typecasting from a numeric string would be the best way, though I wouldn't cast it to an integer unless you know with absolute certainly the value will always be "integer". I would cast it as floating point without knowing this for certain.
PowerBook-G4:~ dreamscape$ php -a
Interactive mode enabled
php > echo (float)'09';
9
php > echo (float)'09.99';
9.99
php >