RosellaC - Ramos-Topic 3-PHP Control Structures-Part 2
RosellaC - Ramos-Topic 3-PHP Control Structures-Part 2
<?php
$a = 0; $b = 0;
for( $i = 0; $i<5; $i++ ) {
$a += 10; $b += 5; }
</body>
</html>
The example below defines a loop that starts
with $i=0. The loop will continued until $i is less
than, 5. The variable $i will increase by 1 each time
the loop runs:
Syntax
foreach (array as value)
{ code to be executed; }
Example
<html>
<body>
<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
?>
</body>
</html>
This will produce the following result :
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
There is one more syntax of foreach loop,
which is extension of the first.
$superhero = array(
"name" => "Peter Parker", "email" =>
"peterparker@mail.com", "age" => 18
);
// Loop through superhero array
}
?>
Output
name : Peter Parker
email : peterparker@mail.com
age : 18