note 106446 added to function.array-rand

From: Date: Thu, 10 Nov 2011 01:32:19 +0000
Subject: note 106446 added to function.array-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-184361@lists.php.net to get a copy of this message
Hi, this code is perfect to me... <?php $n = 15; $data = range(1, 20); $rand = array_rand($data,$n); for($i=0; $i<$n; $i++) { echo $rand[$i]."<br>"; } ?> but I don't know how to fill the number less than 4 size chars. example, if the value is 21, so this displays as 0021 I have used this code, but it don`t works fine <?php $n = 15; $data = range(0, 9999); $num = $data; $rand = array_rand($num,$n); for($i=0; $i<$n; $i++) { if ($rand <= 9){ $num = '000'.$rand; }else if($rand <= 99){ $num = '00'.$rand; }else if ($rand <= 999){ $num = '0'.$rand; }else{ $num = $rand; } echo $num[$i]."<br>"; } ?> Thanks for your help ---- Server IP: 190.90.160.68 Probable Submitter: 190.156.236.62 (proxied: 190.156.236.62) ---- Manual Page -- http://www.php.net/manual/en/function.array-rand.php Edit -- https://master.php.net/note/edit/106446 Del: integrated -- https://master.php.net/note/delete/106446/integrated Del: useless -- https://master.php.net/note/delete/106446/useless Del: bad code -- https://master.php.net/note/delete/106446/bad+code Del: spam -- https://master.php.net/note/delete/106446/spam Del: non-english -- https://master.php.net/note/delete/106446/non-english Del: in docs -- https://master.php.net/note/delete/106446/in+docs Del: other reasons-- https://master.php.net/note/delete/106446 Reject -- https://master.php.net/note/reject/106446 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184361) next »