-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_loops.php
82 lines (60 loc) · 1.27 KB
/
05_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/* -------- Loops & Iteration ------- */
/* ------------ For Loop ------------ */
/*
** For Loop Syntax
for (initialize; condition; increment) {
// code to be executed
}
*/
// for ($i = 0; $i < 5; $i++) {
// echo "The number is: $i <br>";
// }
/* ------------ While Loop ------------ */
/*
** While Loop Syntax
while (condition) {
// code to be executed
}
*/
// $x = 1;
// while ($x <= 5) {
// echo "The number is: $x <br>";
// $x++;
// }
/* ---------- Do While Loop --------- */
/*
** Do While Loop Syntax
do {
// code to be executed
} while (condition);
do...while loop will always execute the block of code once, even if the condition is false.
*/
// $x = 11;
// do {
// echo "The number is: $x <br>";
// $x++;
// } while ($x <= 10);
/* ---------- Foreach Loop ---------- */
/*
** Foreach Loop Syntax
foreach ($array as $value) {
// code to be executed
}
*/
// $posts = ["First post", "Second post", "Third post"];
// for ($i = 0; $i < count($posts); $i++) {
// echo $posts[$i] . "<br>";
// }
// foreach ($posts as $post) {
// echo $post . "<br>";
// }
$person =
[
'first_name' => 'Stefan',
'last_name' => 'Gogov',
'email' => 'stefan@gmail.com'
];
foreach ($person as $key => $value) {
echo "$key: $value <br>";
}