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); ? >
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
+ 5
I fixed some sintax errors.
https://code.sololearn.com/wzxqbtE7xyZy/?ref=app
+ 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.
+ 4
Read about Loop and Functions
https://www.sololearn.com/learn/PHP/1834/
https://www.sololearn.com/learn/PHP/1826/
+ 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