-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_loops.php
41 lines (36 loc) · 714 Bytes
/
07_loops.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
// while
// while(true){
// }
// Loop with $counter
$counter = 0;
while ($counter < 10) {
echo "Counter $counter" . "<br>";
$counter++;
}
// do - while
$counter = 10;
do {
echo "Counter $counter";
} while ($counter < 10);
// for
for ($i = 0; $i < 10; $i++) {
echo "Counter: $i" . "<br>";
}
// foreach
$fruits = ["Banana", "Apple", "Orange"];
foreach ($fruits as $i => $fruit) {
echo $i . " " . $fruit . "<br>";
}
// Iterate Over associative array.
$person = [
"name" => "Brand",
"surname" => "Traversy",
"age" => 30,
"hobbies" => ["Tennis", "Video Games"]
];
foreach ($person as $key => $value) {
if ($key === "hobbies")
break;
echo $value . "<br>";
}