Repository navigation
Expand file tree
/
Copy pathloops.php
More file actions
214 lines (175 loc) · 4.63 KB
/
Copy pathloops.php
File metadata and controls
214 lines (175 loc) · 4.63 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
// for each loop
$colors = ['red', 'blue', 'green'];
foreach($colors as $key => $color) {
var_dump($key); // or you can also use $key to show the the numeric array object in a loop
// you can also replace the word key to any different name (it's still defined as a key even if you change the variable name) you want but it is still a key to determine a variable
}
?>
<?php
$invoiceItems = [
['item' => 'Monitor', 'price' => 1200],
['item' => 'Mouse', 'price' => 150],
['item' => 'Keyboard', 'price' => 100]
];
$totals = 0;
foreach($invoiceItems as $item) {
$totals = $totals + $item['price'];
}
var_dump($totals);
echo $totals;
?>
<?php
// other example
$invoiceItems2 = [
['item' => 'Monitor', 'price' => 1200],
['item' => 'Mouse', 'price' => 150],
['item' => 'Keyboard', 'price' => 100]
];
$totals2 = 0;
foreach($invoiceItems2 as $item2) {
$totals2 += $item2['price'];
}
var_dump($totals2);
echo $totals2;
?>
<?php
// for loop
for ($i=0; $i < 10 ; $i++) {
var_dump($i);
}
?>
<?php
// while loop
$count = 0;
while ($count <= 10) {
var_dump($count);
$count++;
}
?>
<?php
// while loop p.2
$count2 = 10;
while ($count2 >= 0) {
var_dump($count2);
$count2--;
/*
without updating the counter variable ($count), it will be an endless loop if its condition
remains always true.
by using the counter variable either addition or subtraction after iteration,
it will count from number to number either ascending or descending as long as
the bool argument is true, e.g. 10 >= 0 then $count--;
vice versa if you will use $count++ on a 0 <= 10 $count++;
*/
}
?>
<?php
// do while loop
$counts = 1000;
do {
var_dump($counts);
$counts++;
// this is run no matter what.
}
while($counts <= 5);
/*
in do while loop, doing something (the dump) before it checks the iteration makes
the loop output be a variable type with the value.
*/
?>
<?php
// other example
// setting associative boolean array
$users = [
[
'name'=>'Matthew', 'with honors'=>true
],
[
'name'=>'Mark', 'with honors'=>false
],
[
'name'=>'Luke', 'with honors'=>true
]
];
// this checks first the boolean array if true then it will dump data which contains true
foreach($users as $user) {
// this gets the true bool argument and dumps them
if ($user['with honors']) {
var_dump('Give Certificate to' . ' ' . $user['name']);
}
// while this gets the false bool argument
else if (!$user['with honors']) {
var_dump('Do not give certificate to' . ' ' . $user['name']);
}
/*
even without double checking again with an else if... ! statement,
just by using else, it will check and determine whether the boolean
object array is false.
(you don't have to do this) but it's another way but not recommended
else if (!$user['with honors']) {
var_dump('Do not give certificate to' . ' ' . $user['name']);
}
/* note that even though the if and else if statement have a different conditions,
foreach still checks each user according to their order in the array.
for each user, it checks whether 'with honors' is true or false
and gives the corresponding output.
*/
}
?>
<?php
// another foreach loop but with continue
$users2 = [
[
'name'=>'Matthew', 'with honors'=>true
],
[
'name'=>'Mark', 'with honors'=>false
],
[
'name'=>'Luke', 'with honors'=>true
]
];
foreach ($users2 as $user2) {
// continue loop with negation
if (!$user2['with honors']) {
continue;
}
var_dump('Give Certificate to' . ' ' . $user2['name']);
/* you can also control with the current loop or not, by continuing you can use continue; but
in this example, it continues with those who are with honors since the syntax identifies
with a negation that is not users and remove them.
*/
}
?>
<?php
// another foreach loop but with break
$users3 = [
[
'name'=>'Matthew', 'with honors'=>true
],
[
'name'=>'Mark', 'with honors'=>false
],
[
'name'=>'Luke', 'with honors'=>true
]
];
foreach ($users3 as $user3) {
// break loop with negation
if (!$user3['with honors']) {
break;
}
var_dump('Give Certificate to' . ' ' . $user3['name']);
/* with using break, the loop breaks if after a true associative array object is false.
by using this, it is for you to control your loop.
*/
}
?>
<?php
// last loop example
$numbers1 = [1, 2, 3, 4, 5];
$doubled = [];
foreach($numbers1 as $numbers) {
$doubled[]=$numbers*2;
}
?>