zerofill

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

zerofill

Post by chris12295 »

i want this

$num = 615;

to change to this

$num = 0615;

the numbers come from an array so i dont know how long they are, i just know they are no more than 4 digits.

is there any way to zerofill 1-3 digit numbers?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Not as integers. Prepend a "0" as a string

Code: Select all

<?php
$bla=615;
$num="0".$bla;
?>
Note: $num is a string, not an integer.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can use sprintf():

Code: Select all

<?php
$test = 93;
$test_padded = sprintf('%04d', $test);
echo $test_padded;
?>
Mac
Post Reply