-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp.php
More file actions
333 lines (277 loc) · 6.79 KB
/
Copy pathphp.php
File metadata and controls
333 lines (277 loc) · 6.79 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// PHP code
// generates coin flips until 3 heads are reached
<!DOCTYPE html>
<html>
<head>
<title>A loop of your own</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
//Add while loop below
$flipCount = 0;
$headCount = 0;
while($headCount < 3) {
$headCount = $headCount + rand(0,1);
$flipCount ++;
echo "This is flipCount: " . $flipCount;
echo "This is headCount: " . $headCount;
}
?>
</body>
</html>
<html>
<p>
<?php
// Use rand() to print a random number to the screen
print rand();
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Richard";
$nameLen = strlen($name);
$nameRandPos = rand(0, $nameLen);
echo "This is $nameRandPos";
$randLetter = substr($name, $nameRandPos, 1);
echo "This is matching letter: " . $randLetter;
?>
</p>
</html>
<html>
<p>
<?php
// Create an array and push on the names
// of your closest family and friends
$fambam = array();
array_push($fambam, "Jae", "Kelly", "Mum", "Greg", "David");
// Sort the list
sort($fambam);
print_r ($fambam);
$arrLen = count($fambam);
// Randomly select a winner!
$randWinner = rand(0, $arrLen);
$winner = $fambam[$randWinner];
// Print the winner's name in ALL CAPS
echo strtoupper($winner) . " IS A WINNER!";
?>
</p>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Reconstructing the Person Class</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<p>
<!-- Your code here -->
<?php
class Person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
}
$teacher = new Person("boring", "12345", 12345);
$student = new Person("notboring", "54321", 54321);
echo $teacher->greet();
echo $student->greet();
?>
</p>
</body>
</html>
<html>
<head>
<title>The Shape of Things to Come</title>
</head>
<body>
<p>
<?php
class Shape {
public $hasSides = true;
}
class Square extends Shape {
}
$square = new Square();
// Add your code below!
if (property_exists($square, "hasSides")) {
echo "I have sides!";
}
?>
</p>
</body>
</html>
<html>
<head>
<title>Override!</title>
</head>
<body>
<p>
<?php
class Vehicle {
public function honk() {
return "HONK HONK!";
}
}
// Add your code below!
class Bicycle extends Vehicle {
public function honk() {
return "Beep beep!";
}
}
$bicycle = new Bicycle();
echo $bicycle->honk();
?>
</p>
</body>
</html>
<html>
<head>
<title>Scope it Out!</title>
</head>
<body>
<p>
<?php
class Person {
}
class Ninja extends Person {
// Add your code here...
const alive = true;
const stealth = "MAXIMUM";
}
// ...and here!
if(Ninja::alive) {
echo "Not dead yet";
}
echo Ninja::stealth
?>
</p>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<p>
<?php
class King {
// Modify the code on line 10...
public static function proclaim() {
echo "A kingly proclamation!";
}
}
// ...and call the method below!
King::proclaim();
?>
</p>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<p>
<?php
class Person {
public static function say() {
echo "Here are my thoughts!";
}
}
class Blogger extends Person {
const cats = 50;
}
Blogger::say();
echo Blogger::cats;
?>
</p>
</body>
</html>
<html>
<head>
<title>Associate Arrays</title>
</head>
<body>
<p>
<?php
// This is an array using integers as the indices...
$myArray = array(2012, 'blue', 5);
// ...and this is an associative array:
$myAssocArray = array('year' => 2012,
'colour' => 'blue',
'doors' => 5);
// This code will output "blue"...
echo $myArray[1];
echo '<br />';
// ... and this will also output "blue"!
echo $myAssocArray['colour'];
?>
</p>
</body>
</html>
<html>
<head>
<title>Iteration Nation</title>
</head>
<body>
<p>
<?php
$food = array('pizza', 'salad', 'burger');
$salad = array('lettuce' => 'with',
'tomato' => 'without',
'onions' => 'with');
// Looping through an array using "for".
// First, let's get the length of the array!
$length = count($food);
// Remember, arrays in PHP are zero-based:
for ($i = 0; $i < $length; $i++) {
echo $food[$i] . '<br />';
}
echo '<br /><br />I want my salad:<br />';
// Loop through an associative array using "foreach":
foreach ($salad as $ingredient=>$include) {
echo $include . ' ' . $ingredient . '<br />';
}
echo '<br /><br />';
// Create your own array here and loop
// through it using foreach!
$dogs = array('Bob' => 'Schnauser',
'Ed' => 'Rottie',
'Jo' => 'Lab');
foreach ($dogs as $name=>$breed) {
echo 'Yo! Ma name is ' . $name . ' and I\'m a ' . $breed . '<br />';
}
?>
</p>
</body>
</html>
<html>
<head>
<title>Blackjack!</title>
</head>
<body>
<p>
<?php
$deck = array(array('2 of Diamonds', 2),
array('5 of Diamonds', 5),
array('7 of Diamonds', 7),
array('8 of Diamonds', 8);
// Imagine the first chosen card was the 7 of Diamonds.
// This is how we would show the user what they have:
echo 'You have the ' . $deck[2][0] . '!';
?>
</p>
</body>
</html>