Angka Nol depan angka


<?

$angka = 123;
// hasilnya nanti 000001
//kalo 12 jadinya 000012
$len = strlen($angka);
if($len==1) $nol=”00000″;
else if($len==2) $nol=”0000″;
else if($len==3) $nol=”000″;
else if($len==4) $nol=”00″;
else if($len==5) $nol=”0″;

$angka = $nol.$angka;

echo $angka;

?>

Kode di atas, pasti sudah benar tapi bagaimana dengan kode seperti ini :

<?

$angka = 12322234;
$nol = array(1=>”00000″,”0000″,”000″,”00″,”0″,””);
$angka= $nol[strlen($angka)].$angka;
echo “$angka”;

?>

bagaimana kalau dgn ini:

$angka = “123″;
echo sprintf(“%06d”, $angka);

About blackphp

All about programming php, ajax, jquery, mysql, etc, tips n trick. Now, i'm concern in php programming with codeigniter framework n also training my design skill with photoshop or corel... wish me luck! ('o')

Posted on August 19, 2010, in PHP and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: