Page 1 of 1

Zeros before numbers...

Posted: Mon Oct 23, 2006 4:25 pm
by tommy1987
Hi I am trying to assign values to numbers e.g. 01 instead of 1 like so:

$number = 01;

However PHP takes this to just be 1, how do I force it to keep the zero before it?

Posted: Mon Oct 23, 2006 4:33 pm
by Z3RO21
treat it as a string

Code: Select all

$Var = '01';

Posted: Mon Oct 23, 2006 5:15 pm
by RobertGonzalez
Why do you need the 0's? Moving an integer to a string might offer unexpected or unwanted results.

Posted: Mon Oct 23, 2006 7:41 pm
by hawleyjr
You can always try using sprintf()

Here is a reverse example using currency:

Code: Select all

<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>

Posted: Tue Oct 24, 2006 3:10 am
by twigletmac
You may also find str_pad() useful but, to echo Everah, be wary of how you apply this.

Mac