-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.php
More file actions
145 lines (102 loc) · 3.22 KB
/
loops.php
File metadata and controls
145 lines (102 loc) · 3.22 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
<h1>Loops are a logic structure that is useful to repeat a block of instructions several times. foreach, while, for are the three different looping structures.</h1>
<p style="color: red;">For each element of the array $plates, call it $plate then apply the instruction : wash_plate().</p>
<?php
$names = array('John', 'jeanne', 'Joan', 'émile');
foreach ($names as $name) {
echo ucfirst($name);
}
?>
<hr>
<p style="color: red;">Foreach, with the key index.</p>
<?php
$names = array('John', 'jeanne', 'Joan', 'émile');
var_dump($names);
foreach ($names as $key => $value) {
$names[$key] = ucfirst($value);
}
var_dump($names);
?>
<hr>
<p style="color: red;">Using foreach, build a loop that displays each element of the array.</p>
<?php
$pronouns = array('I', 'You', 'He/She', 'We', 'You', 'They');
foreach ($pronouns as $key => $value) {
$pronouns[$key] = print_r($pronouns);
}
?>
<hr>
<p style="color: red;">Then, modify your loop so as to conjugate the verb "to code" in the present tense. Use a <br> to go to the next line. The result should be :
I code
You code
He/She codes
We code
You code
They code</p>
<?php
$pronouns = array('I', 'You', 'He/She', 'We', 'You', 'They');
foreach ($pronouns as $key => $value) {
if ($pronouns[$key] == 'He/She') {
array_push($pronouns, $pronouns[$key], 'codes');
} else {
array_push($pronouns, $pronouns[$key],'code');
}
}
print_r($pronouns)
?>
<hr>
<p style="color: red;">You can also loop inside a string
While loop
Bart Simpson has been (again) punished : he must copy 100 times "I shall not watch flies fly when I'm learning PHP".
Let's use another loop structure to do just that: the Whileloop</p>
<?php
$amount_of_lines = 1;
while ($amount_of_lines <= 100) {
echo $amount_of_lines . '. : I shall not watch flies fly when I am learning PHP.';
// shorthand writing for
// $amount_of_lines = $amount_of_lines +1;
$amount_of_lines++;
}
?>
?>
<hr>
<p style="color: red;">For loop
Here is a third looping structure: the For loop. Check this out :</p>
<?php
for ($amount_of_lines = 1; $amount_of_lines <= 100; $amount_of_lines++) {
echo $amount_of_lines . '. : I shall not watch flies fly when I am learning PHP.<br />';
}
?>
<hr>
<h1>Exercises</h1>
<p style="color: red;">Write a script that prints the numbers from 1 to 120 using while
</p>
<?php
$n = 1;
while ($n <= 120) {
echo $n++;
}
?>
<hr>
<p style="color: red;">Write a script that prints the numbers from 1 to 120 using for
</p>
<?php
for ($n = 1; $n <= 120; $n++) {
echo $n;
}
?>
<hr>
<p style="color: red;">Create an array containing at least 10 countries. Then, generate the html that will render a select box inside an html form (see mockup below). Of course, a loop will be useful...
</p>
<?php
$pays = array('BE' => 'Belgique', 'BZ' => 'Belize', 'BO' => ' The Plurinational State of Bolivia', 'AT' => 'Austria', 'EE' => 'Estonia', 'FI' => 'Finland', 'FR' => 'France', 'HT' => 'The Republic of Haiti', 'HN' => 'Hinduras', 'IS' => 'Island');
?>
<hr>
<form type="get">
<select name='pays'>
<?php
foreach ($pays as $iso => $pay) {
echo "<option value='$iso'>$pay</option>";
}
?>
</select>
</form>