View Full Version : Output numbers 01 instead of 1 in php

Len Whistler
04-10-2004, 10:04 PM
I am creating a php script to randomly output numbers from 01 - 49 but I want the output to be 2 digits. Example: 01 02 03 etc, not 1 2 3. I have googled this and checked the manual but I can't find it, I know its a simple solution but I must be typing the wrong search words.

If someone knows the solution please post it. Thanks.

Leonard Whistler

04-10-2004, 10:20 PM
You could do something like

$str = 9;
if($str < 10){
$str = "0".$str;
echo $str;

04-11-2004, 12:19 AM
Yea I needed to do the same thing...

So I just have something like:


Whenever it gets over 9 I change and exclude the zero part.

Len Whistler
04-11-2004, 12:25 AM
Thanks Nightfire and dniwebdesign........

...this forum is very good for solutions like this.

Leonard Whistler

04-11-2004, 02:57 AM
also worth a looksee ...

echo str_pad($str, 2, "0", STR_PAD_LEFT) ;

04-11-2004, 02:58 AM
Oooh, a new function for me. *looks it up*

<edit>That's one useful function. Might come in handy soon</edit>