/
index.php
558 lines (400 loc) · 11.9 KB
/
index.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
//phpinfo();
require 'functions.php';
require 'database.php';
// used for single line comments
/*
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
//must be repeated for multiply line..
/* and are used to delineate a comment block */
/* used once at the beginning
used once at the end*/
// variables
use \absolom\my_stuff\Database;
use \absolom\my_stuff\Foo;
$foo = new Database();
$fooF = new Foo();
$foo->doAwesomeThings();
$fooF->doAwesomeThings();
//$obj = new absolom\my_stuff\Foo();
//$objDb = new absolom\my_stuff\Database();
//echo $obj->doAwesomeThings();
//echo $objDb->doAwesomeThings();
$a = 1;
++$a;
$a *= $a;
echo $a-- . '<br>';
echo 'Welcome to Zend Certication pactice' . '<br>';
$m = 4 % 3;
echo $m . '<br>';
$age = 21;
// Control structures
/* if ( $age < 18) {
echo 'child';
} else {
echo 'adult';
} */
// Tenary operator
/*
Ternary operator logic is the process of using "(condition) ? (true return value) : (false return value)"
statements to shorten your if/else structures.
*/
// $agestr = ($age < 16) ? 'child' : 'adult';
echo ($age < 18 ) ? 'child' : 'adult';
$x = 1;
while ($x <= 18) {
echo "The number is:".$x."<br>";
$x++;
}
$y = 1;
do {
echo "The number is: $y <br>";
$y++;
} while ($y <= 5);
for ($z = 0; $z <= 10; $z++) {
echo "The number is: $z <br>";
}
$colors = array("red", "green", "blue", "yellow");
$arrlength = count($colors);
for($x = 0; $x < $arrlength; $x++) {
echo $colors[$x];
echo "<br>";
}
//PHP Associative Arrays
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
//Loop Through an Associative Array
foreach ($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
//PHP - Multidimensional Arrays
//PHP - Two-dimensional Arrays
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
$Cararrlength = count($cars);
for ($row = 0; $row < $Cararrlength; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
//echo dirname(__FILE__);
/*
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
*/
//Create a User Defined Function in PHP
/*
function functionName() {
code to be executed;
}
*/
function writeMsg(){
echo 'Hello world!'.'<br>';
}
writeMsg();
//PHP Function Arguments
function familyName($fname){
echo "$fname Mugwagwa.<br>";
}
familyName('Absolom');
familyName('Makabongwe');
familyName('Alison');
//The following example has a function with two arguments ($fname and $year):
function familyNameYear($fname,$year){
echo "$fname Mugwagwa. Born in $year<br> ";
}
familyNameYear('Absolom','1985');
familyNameYear('Makabongwe','1984');
familyNameYear('Alison','2015');
//PHP Default Argument Value
//The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:
function setHeight($minHeight = 50) {
echo "The height is : $minHeight <br>";
}
setHeight(350);
setHeight(250);
setHeight(150);
setHeight(100);
setHeight();
//PHP Functions - Returning values
//To let a function return a value, use the return statement:
function sum($x,$y){
$z = $x + $y;
return $z;
}
echo sum(3,2) . "<br>";
echo sum(5, 10) . "<br>";
echo sum(7, 13) . "<br>";
echo sum(2, 4) . "<br>";
//Classes
class MyClass {
public function __construct()
{
echo 'The class "', __CLASS__, '" was initiated!<br />';
}
public function __destruct()
{
//echo 'The class "', __CLASS__, '" was destroyed.<br />';
}
public $prop1 = "I'm a class property";
public function setProperty($newval){
$this->prop1 = $newval;
}
public function getProperty() {
return $this->prop1 . "<br />";
}
}
$obj = new MyClass();
$obj2 = new MyClass();
echo $obj->getProperty();
echo $obj2->getProperty();
$obj->setProperty("Son! You're close to becoming a developer");
$obj2->setProperty("Son! Welldone!");
echo $obj->getProperty();
echo $obj2->getProperty();
//$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
//echo fread($myfile,filesize("webdictionary.txt"));
//fclose($myfile);
//Using Class Inheritance
class MyOtherClass extends MyClass {
public function __construct()
{
/*
Preserving Original Method Functionality While Overwriting Methods
To add new functionality to an inherited method while keeping the original method intact, use the parent keyword with the scope resolution operator (::):
*/
parent::__construct(); // Call the parent class's constructor
echo "A new constructor in " . __CLASS__ . ".<br />";
}
public function newMethod()
{
echo "From a new method in " . __CLASS__ . ".<br />";
}
}
$obj3 = new MyOtherClass();
echo $obj3->newMethod();
$obj3->setProperty("Son! this is impressive");
echo $obj3->getProperty();
/**
Reason 1: Ease of Implementation
"While it may be daunting at first, OOP actually provides an easier approach to dealing with data."
While it may be daunting at first, OOP actually provides an easier approach to dealing with data. Because an object can store data internally, variables don't need to be passed from function to function to work properly.
Also, because multiple instances of the same class can exist simultaneously, dealing with large data sets is infinitely easier. For instance, imagine you have two people's information being processed in a file. They need names, occupations, and ages.
The Procedural Approach
Here's the procedural approach to our example:
**/
function changeJob($person, $newjob) {
$person['job'] = $newjob; // Change the person's job
return $person;
}
function happyBirthday($person)
{
++$person['age']; // Add 1 to the person's age
return $person;
}
$person1 = array(
'name' => 'Tom',
'job' => 'Button-Pusher',
'age' => 34
);
$person2 = array(
'name' => 'John',
'job' => 'Lever-Puller',
'age' => 41
);
// Output the starting values for the people
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
// Tom got a promotion and had a birthday
$person1 = changeJob($person1, 'Box-Mover');
$person1 = happyBirthday($person1);
// John just had a birthday
$person2 = happyBirthday($person2);
// Output the new values for the people
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
/**
While this code isn't necessarily bad, there's a lot to keep in mind while coding. The array of the affected person's attributes must be passed and returned from each function call, which leaves margin for error.
To clean up this example, it would be desirable to leave as few things up to the developer as possible. Only absolutely essential information for the current operation should need to be passed to the functions.
This is where OOP steps in and helps you clean things up.
The OOP Approach
Here's the OOP approach to our example:
**/
class Person
{
private $_name;
private $_job;
private $_age;
public function __construct($name, $job, $age)
{
$this->_name = $name;
$this->_job = $job;
$this->_age = $age;
}
public function changeJob($newjob)
{
$this->_job = $newjob;
}
public function happyBirthday()
{
++$this->_age;
}
}
// Create two new people
$person1 = new Person("Tom", "Button-Pusher", 34);
$person2 = new Person("John", "Lever Puller", 41);
// Output their starting point
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
// Give Tom a promotion and a birthday
$person1->changeJob("Box-Mover");
$person1->happyBirthday();
// John just gets a year older
$person2->happyBirthday();
// Output the ending values
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
//passing varibles by reference
//PHP: Pass by reference vs. Pass by value
function pass_by_value($stack) {
array_push($stack, 4, 5);
}
$stack = array(1,2,3);
//array_push($stack, 4, 5);
pass_by_value($stack);
foreach ($stack as $elem) {
print "<br>$elem";
}
/*
The code above prints 1, 2, 3. This is because the array is passed as value.
*/
function pass_by_reference(&$stack) {
array_push($stack, 4, 5);
}
$stacked = array(1,2,3);
//array_push($stack, 4, 5);
pass_by_reference($stacked);
foreach ($stacked as $elem) {
print "<br>$elem";
}
/*
The code above prints 1, 2, 3, 4, 5. This is because the array is passed as reference, meaning that the function (pass_by_reference) doesn't manipulate a copy of the variable passed, but the actual variable itself. In order to make a variable be passed by reference, it must be declared with a preceeding ampersand (&) in the function's declaration.
difference between passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this copy, so if you change it in the function it won't be changed outside of it.
*/
define("GREETING","Hello you! How are you today?");
echo constant("GREETING");
define('CONSTANT', 1);
define('_CONSTANT', 1);
define('EMPTY', '');
$a = array(
1 => 'php',
'Hypertext',
'Preprocessor',
'widely used' =>
array(
'general' => 'purpose',
'scripting' => 'language',
'that' => 'was',
'originally' =>
array(
5 => 'designed',
9 => 'for',
'Web development',
4 => 'purpose',
)
)
);
//write code here
echo '<pre>';
print_r($a);
echo '</pre>';
echo $a['widely used']['originally'][10];
$array1 = array ('a' => 20, 30, 35);
$array2 = array ('a' => 20, 35, 30);
$array3 =array_intersect_assoc($array1,$array2);
echo '<pre>';
print_r($array3);
echo '</pre>';
$dom = new DOMDocument();
$dom->load('xml/simple.xml');
$a = $dom->documentElement;
echo '<pre>';
print_r($a);
echo '</pre>';
//echo strtotime("january 1, 1901");
//Php date functions
$today = date('l d-m-y h:i:s');
print $today . '<br>';
//regular expressions
if (preg_match("/ell/", "Hello Wolrd!", $matches)) {
echo "Match was found <br />";
echo $matches[0] . '<br/>';
}
//Nested for loops
for ($x = 1; $x <= 2; $x++) {
for ($y = 1; $y <= 3; $y++) {
if($x == $y)continue;
print("x = $x y = $y");
}
}
//Variable casting
var_dump( (bool) 5.8 );
$x =25;
while($x<10)
{
$x--;
}
print ($x);
//$a = `ls -l`;
//echo $a;
//define('FOO', 10);
//$array = array(10 => FOO,"FOO" => 20);
//echo $array[FOO];
//print $array[$array[FOO]] * $array["FOO"];
function b($a = 4)
{
$a = $a / 2;
return $a;
}
$a = 10;
b($a);
echo b($a) . '<br>';
echo 0x33, ' birds sit on ', 022, ' trees.' . '<br>';
$xml = simplexml_load_file('xml/simple.xml') or die('Error cannot create object');
print_r($xml);
//Strings
//Heredoc and Nowdoc
$sausage = 'sausage';
$heredocs = <<<MYSTRING
this is is problem
you suck
$sausage
at the same
MYSTRING;
$Nowdoc = <<<'MYSTRINGNOW'
this is is problem
you suck
$sausage
at the same
MYSTRINGNOW;
echo $heredocs . '<br>';
echo $Nowdoc . '<br>';
$myfile = fopen('newfile.txt','w') or die('Unable to open file!');
$txt = "John Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>