php forloop code example

Example 1: php loop through array

$clothes = array("hat","shoe","shirt");
foreach ($clothes as $item) {
	echo $item;
}

Example 2: for loop php

<?php
	$fruits = ["apple", "banana", "orange"];
	for($i=0;$i<count($fruits);$i++){
    echo "Index of ".$i."= ".$fruits[$i]."<br>";
    }
  ?>

Example 3: php for

<?php
/* example 1 */

for ($i = 1; $i <= 10; $i++) {
    echo $i;
}

/* example 2 */

for ($i = 1; ; $i++) {
    if ($i > 10) {
        break;
    }
    echo $i;
}

/* example 3 */

$i = 1;
for (; ; ) {
    if ($i > 10) {
        break;
    }
    echo $i;
    $i++;
}

/* example 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>

Example 4: for loop in php

/*
For loop in php
*/

<?php
for ($i = 0; $i < 10; $i++) {
     echo $i."<br>";
} 
?>

Example 5: for php

<?php
	$fruits = ["apple", "banana", "orange"];
	for ($i = 0; $i < count($fruits); $i++) {
      	echo "Index of ".$i."= ".$fruits[$i]."<br>";
    }
	// or
	$i = 0;
	foreach ($fruits as $value) {
    	echo "Index of ".$i."= ".$value."<br>";
      	$i++;
	}
?>

Example 6: php loops

#Loops

<?php
    #loops execute code a set number of times
    /*
    Types of loops
    1-For
    2-While
    3-Do..while
    4 Foreach
    */

    # For Loop usually use if you know the number of times it has to execute
    # @params -it takes an init, condition, increment
    #for($i =0;$i<=11;$i++){
    #echo 'Number: '.$i;
    #echo '<br>';
    #}
    #While loop
    # @ prams - condition
    #$i = 0;
    #while($i < 10){
    #    echo $i;
    #    echo '<br>';
    #    $i++;
    #}
    # Do...while loops
    #@prapms - condition
    /*$i = 0;
    do{
        echo $i;
        echo '<br>';
        $i++;
        }
        while($i < 10);*/
    # Foreach  --- is for arrays
    
    # $people = array('Brad', 'Jose', 'William');
    #     foreach($people as $person){
    #     echo $person;
    #     echo '<br>';
    # }
    $people = array('Tony' => '[email protected]',
        'Jose' => '[email protected]','William' => '[email protected]');
    
     foreach($people as $person => $email){
        echo $person.': '.$email;
        echo '<br>';
}
?>