xxxxxxxxxx
<?php
//FOR each item within the array, "ECHO" out the index ($i) and value of each item.
$numbersArray = [1, 2, 3]
for($i = 0; $i < count($numbersArray); $i++) {
echo "Index of ".$i."= ".$numbersArray[$i]."<br>";
}
?>
xxxxxxxxxx
<?php
$fruits = ["apple", "banana", "orange"];
for($i=0;$i<count($fruits);$i++){
echo "Index of ".$i."= ".$fruits[$i]."<br>";
}
?>
xxxxxxxxxx
/*
For loop in php
*/
<?php
for ($i = 0; $i < 10; $i++) {
echo $i."<br>";
}
?>
xxxxxxxxxx
for($i = 0;$i < count($users);$i++) {
if($users[$i]['user_id']==3){
$userName = $users[$i]['first_name'];
}
}
xxxxxxxxxx
Although not specifically pointed out in the main text, escaping from HTML also applies to other control statements:
<?php for ($i = 0; $i < 5; ++$i): ?>
Hello, there!
<?php endfor; ?>
When the above code snippet is executed we get the following output:
Hello, there!
Hello, there!
Hello, there!
Hello, there!