0

How the function work in a php??

<?php function RandomNumber($length) { $str =""; for($i=0;$i<$length;$i++){ $str.=mt_rand(0,9); } return $str; } echo RandomNumb(9); ? >

10th Feb 2020, 10:23 AM
Yaro Ka Yar
Yaro Ka Yar - avatar
5 Answers
+ 3
//It works like this <?php #Here function randomNumber is taking argument 9 as $length vairable function RandomNumber($length) { $str =""; #loops 9 times because $length variable is 9 for($i=0;$i<$length;$i++){ #so this function run's 9 times #mt_rand produce number between 0 to 9 , 9 times as loops remains 9 times $str.=mt_rand(0,9); } #here returns #Here returns $str vairables Which are 9 random numbers return $str; } #Here prints function returned str and passing 9 as arguments echo RandomNumber(9) ?> //I hope you got idea, for better understanding see courses from here https://www.sololearn.com/learn/PHP/1835/ above code's work likes running mt_rand() function 9 times echo mt_rand(0,9); echo mt_rand(0,9); echo mt_rand(0,9); ... //I have also explained and fixed in your other question https://www.sololearn.com/Discuss/2164793/?ref=app
10th Feb 2020, 11:37 AM
Sudarshan Rai
Sudarshan Rai - avatar
10th Feb 2020, 11:33 AM
Pedro H.J
Pedro H.J - avatar
+ 4
This function It will return a random value. Example RandomNumber(9) $length = 9 then the FOR loop will test $i < 9 ? If is true then repeat but it is false then it stops.
10th Feb 2020, 11:40 AM
Pedro H.J
Pedro H.J - avatar
10th Feb 2020, 11:42 AM
Pedro H.J
Pedro H.J - avatar
+ 3
Sudarshan Rai 👑 transfer your answer in this thread so it's not get lost as it's posted before I'll marked the other one for mfd
10th Feb 2020, 3:29 PM
MsJ
MsJ - avatar