1<?php
2 $fruits = ["apple", "banana", "orange"];
3 for($i=0;$i<count($fruits);$i++){
4 echo "Index of ".$i."= ".$fruits[$i]."<br>";
5 }
6 ?>
1/*
2For loop in php
3*/
4
5<?php
6for ($i = 0; $i < 10; $i++) {
7 echo $i."<br>";
8}
9?>
1#Loops
2
3<?php
4 #loops execute code a set number of times
5 /*
6 Types of loops
7 1-For
8 2-While
9 3-Do..while
10 4 Foreach
11 */
12
13 # For Loop usually use if you know the number of times it has to execute
14 # @params -it takes an init, condition, increment
15 #for($i =0;$i<=11;$i++){
16 #echo 'Number: '.$i;
17 #echo '<br>';
18 #}
19 #While loop
20 # @ prams - condition
21 #$i = 0;
22 #while($i < 10){
23 # echo $i;
24 # echo '<br>';
25 # $i++;
26 #}
27 # Do...while loops
28 #@prapms - condition
29 /*$i = 0;
30 do{
31 echo $i;
32 echo '<br>';
33 $i++;
34 }
35 while($i < 10);*/
36 # Foreach --- is for arrays
37
38 # $people = array('Brad', 'Jose', 'William');
39 # foreach($people as $person){
40 # echo $person;
41 # echo '<br>';
42 # }
43 $people = array('Tony' => 'tony@example.com',
44 'Jose' => 'jose@example.com','William' => 'William@example.com');
45
46 foreach($people as $person => $email){
47 echo $person.': '.$email;
48 echo '<br>';
49}
50?>
1for (initialization; condition; increment){
2 code to be executed;
3}
4//Example
5<?php
6 $a = 0;
7 $b = 0;
8 for( $i = 0; $i<5; $i++ )
9 {
10 $a += 10;
11 $b += 5;
12 }
13echo ("At the end of the loop a = $a and b = $b" );
14?>
15//.......................................//
16 do {
17 code to be executed;
18}
19while (condition);
20//Example
21<?php
22 $i = 0;
23 $num = 0;
24
25 do {
26 $i++;
27 }
28
29 while( $i < 10 );
30 echo ("Loop stopped at i = $i" );
31?>