FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Filling an array with random input doesn't quite work
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: solved! Re: Filling an array with random input doesn't quite work [message #185257 is a reply to message #185219] Fri, 14 March 2014 22:23 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Christoph Michael Becker wrote:

> Tim Streater wrote:
>> richard <noreply(at)example(dot)com> wrote:
>>> $x=1;
>>> while ($x<40){
>>> $yr=rand(1,10);
>>> $yr=$yr+59;
>>> $num=rand(1,100);
>>> if ($num<100){$num="0".$num;}
>>> if ($num<10){$num="0".$num;}
>>
>> What is the point if this second "if"? You've already covered that with
>> the first.
>
> That serves to add padding to three characters. The last three lines
> could be replaced with the following:
>
> str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);

$num = str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);

as str_pad() does not modify strings (because they are immutable).

Or simply

$num = sprintf('%03d', rand(1, 100));

:)


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: readdir lists randomly
Next Topic: Nested PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 20:55:08 GMT 2024

Total time taken to generate the page: 0.04367 seconds