Re: solved! Re: Filling an array with random input doesn't quite work [message #185258 is a reply to message #185257] |
Fri, 14 March 2014 22:58 |
Christoph Michael Bec
Messages: 207 Registered: June 2013
Karma:
|
Senior Member |
|
|
Thomas 'PointedEars' Lahn wrote:
> 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).
Of course! Thanks for the correction.
> Or simply
>
> $num = sprintf('%03d', rand(1, 100));
>
> :)
Even better. :)
--
Christoph M. Becker
|
|
|