for loop in php for array code example
Example 1: php loop through array
$clothes = array("hat","shoe","shirt");
foreach ($clothes as $item) {
echo $item;
}
Example 2: php iterate through array
$arr = ['Item 1', 'Item 2', 'Item 3'];
foreach ($arr as $item) {
var_dump($item);
}
Example 3: for loop in php
<?php
for ($i = 0; $i < 10; $i++) {
echo $i."<br>";
}
?>
Example 4: php loops
#Loops
<?php
#loops execute code a set number of times
# 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
# 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>';
}
?>