0

Can any 1 tell me what is use of for loop?

The reason given in tutorial is not justifying me. plz explain thanks

5th Aug 2016, 4:21 AM
Ankita Sharma
Ankita Sharma - avatar
10 Answers
+ 1
A dynamic variable can be a lot of thing but a little exemple is with the native function rand () Who give a random number like rand (1, 10) can return 1 or 2 or 3 etc.. so $a = rand( 1 , 10 ); for ($i = 0; $i <$a ; $i++) { echo ($i . "</br>"); } if $a = 3 this code print: 0 1 2 and if $a = 1 this code print: 0 dynamic variable can be a result of a request to database or the timestamp with time () used for exemple to print the date in real time or anythink
5th Aug 2016, 9:51 AM
francis
+ 1
For loop is used mainly for two purposes: 1. Repeating a block of code a number of times. 2. Iterating through the values of an array
5th Aug 2016, 5:21 PM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
0
it used to do a loop on a fixed number or a dynamic variable for a lot of thing and not hardcode all possibility
5th Aug 2016, 5:45 AM
francis
0
ohh okay thanks a lot
5th Aug 2016, 5:47 AM
Ankita Sharma
Ankita Sharma - avatar
0
can u also give eg for dynamic variable
5th Aug 2016, 5:49 AM
Ankita Sharma
Ankita Sharma - avatar
0
thanks again
5th Aug 2016, 9:54 AM
Ankita Sharma
Ankita Sharma - avatar
0
your welcome ankita
5th Aug 2016, 9:59 AM
francis
0
to make iterative code easy..it assign value to variable also give limit and also increament/decreament it in single line code.
16th Aug 2016, 8:03 AM
Bharati Deepak Giri
0
i you want some calculations in repeating order loops will help you eg if you want to add first 10 natural number without loops you can add them but it is time consuming with loops you can do it with very few statements eg $num=1; $num2=1; while($num!=10) { $num2+=$num; $num++; }
4th Sep 2016, 11:32 AM
keshav sharma
keshav sharma - avatar
0
Can also be use in generating random number for example BVN or MATRICULATION NUMBER of students
12th Dec 2016, 6:56 PM
Tukele Osareme C
Tukele Osareme C - avatar