forked from bramus/ws1-sws-course-materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.php.intro.html
1647 lines (1476 loc) · 54.2 KB
/
01.php.intro.html
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Webscripting1 — Serverside Webscripting — 01.php.intro</title>
<meta name="description" content="Webscripting1 — Serverside Webscripting — 01.php.intro">
<meta name="author" content="Bram(us) Van Damme - ikdoeict.be">
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css" media="screen">
<link rel="stylesheet" href="css/print.css" media="print">
<link rel="stylesheet" href="lib/zenburn.css">
<style>
.columns .column {
float: left;
list-style: none;
margin: 0;
padding: 0;
}
.column-12 {
width: 50%;
}
code {
color: gainsboro;
}
li > code, li em > code, li del > code, li ins > code, p > code {
background: #3F3F3F;
padding: 2px 4px;
box-shadow: 0px 0px 6px rgba(0,0,0,0.3);
font-size: 80%;
}
del code, code.nok {
color: #C55;
}
ins code, code.ok {
color: #5C5;
}
table {
font-size: 15px;
border-collapse: collapse;
width: 100%;
}
table td, table th {
line-height: 1.4;
border: 1px solid #ccc;
padding: 4px;
}
table th {
background: #333;
}
</style>
</head>
<body>
<div id="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- 0 : Title -->
<section>
<section>
<h3 class="inverted">Serverside Webscripting <small>[JLW322]</small></h3>
<h1>01.php.intro</h1>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
<script>
// Delicously hacky. Look away.
if( navigator.userAgent.match( /(iPhone|iPad|iPod|Android)/i ) )
document.write( '<p style="color: rgba(0,0,0,0.3); text-shadow: none;">('+'Tap to navigate'+')</p>' );
</script>
</section>
</section>
<!-- PHP & Running PHP -->
<section>
<section>
<h2>PHP Introduction</h2>
</section>
<section>
<h2>PHP</h2>
<ul>
<li class="fragment">
Short for <em>PHP: Hypertext Preprocessor</em>
</li>
<li class="fragment" style="margin-top: 1em;">
Scripting language
<ul>
<li>Loose syntax</li>
<li>
Not compiled for distribution (cfr. Java), but interpreted at runtime
<ul>
<li>Therefore it's slower than compiled languages</li>
</ul>
</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em;">
Very popular on the internet
<ul>
<li>One of the first Serverside Scripting Languages (° 1995)</li>
<li>PHP plugins exist for nearly all web servers and OSes</li>
<li>e.g. Wordpress, Facebook, Wikipedia, etc.</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em;">
Suffers from historical cruft & syntax inconsistencies
</li>
</ul>
</section>
<section>
<h2>PHP Versions</h2>
<ul>
<li class="fragment">
Current versions:
<ul>
<li>5.4.33 <em>(*)</em></li>
<li>5.5.16</li>
<li>5.6.0 <em>(new since August!)</em></li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em;">
Our code is PHP 5.4+ compliant
</li>
</ul>
<footer class="fragment"><em>(*) Released Sep, 18 2014. The last planned release that contains regular bugfixes. All the consequent 5.4.* releases will contain only security-relevant fixes, for the term of one year.</em></footer>
</section>
<section>
<h2>Obtaining PHP: Combined</h2>
<ul>
<li class="fragment">
A preconfigured PHP is included in <a href="http://www.wampserver.com/en/">Wampserver</a> / <a href="http://mamp.info/en">MAMP</a>
<ul>
<li>Install one of these for this course</li>
<li>Follow <a href="http://helpdesk.ikdoeict.be/index.php?/Knowledgebase/Article/View/11/0/howto-installatieconfiguratie-van-software-wamplamp-stack">these instructions</a> afer installation</li>
<li>Be sure to add the PHP binary to your system's <code>PATH</code>
<ul>
<li>Windows: Add <code>C:\wamp\bin\php\<em>version</em>\</code> to <code>PATH</code> via Computer Settings</li>
<li>OS X: Paste this on the Terminal: <code>echo 'export PATH=/Applications/MAMP/bin/php/php<em>version</em>/bin/:$PATH' >> ~/.bash_profile</code></li>
</ul>
</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em">
Shipped with most Unix/Linux distributions
</li>
</ul>
</section>
<section>
<h2>Obtaining PHP: Manual</h2>
<ul>
<li class="fragment">
PHP can be downloaded from <a href="http://php.net">http://php.net</a>.
<ul>
<li>Just extract the <code>.zip</code> onto your filesystem and you're good to go</li>
<li>Windows users must download from <a href="http://windows.php.net/">http://windows.php.net/</a></li>
<li>OS X users obtain prebuilt binaries using <a href="https://github.com/josegonzalez/homebrew-php">PHP Homebrew formulae</a> or from <a href="http://php-osx.liip.ch/">Liip AG</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Configuring PHP</h2>
<ul>
<li class="fragment">
PHP can be configured via a file named <code>php.ini</code>
</li>
<li class="fragment" style="margin-top: 1em">We'll take a closer look at this file when it's needed</li>
<li class="fragment" style="margin-top: 1em">Note: the <code>php.ini</code> shipped with Wampserver/MAMP is configured <em>mostly</em> correctly</li>
</ul>
</section>
<section>
<h2>Running PHP (1)</h2>
<ul>
<li class="fragment">
From the shell
<ul>
<li class="fragment">Run <code>php -a</code> to get an interative shell at which you can type in PHP statements
<ul class="fragment">
<li>cfr. the MySQL shell where you type in SQL statements (queries)</li>
<li>A <code>;</code> ends a statement</li>
<li>Type <code>quit</code> or <code>exit</code> to leave the shell</li>
</ul>
<div class="fragment"><pre class="bigger"><code class="language-php">bramus$ <span class="fragment">php -a</span>
<span class="fragment">Interactive shell
php ></span><span class="fragment"> echo 'hello';</span>
<span class="fragment">hello
php ></span><span class="fragment"> quit
bramus$</span></code></pre></div>
</li>
</ul>
</li>
</section>
<section>
<h2>Running PHP (2)</h2>
<ul>
<li>
From the shell <em>(continued)</em>
<ul>
<li class="fragment">
Run <code>php -r "<em>one line of PHP code</em>"</code> to run a single line of PHP code
<div class="fragment"><pre class="bigger"><code class="language-php">bramus$<span class="fragment"> php -r "echo 'hello' . PHP_EOL;"</span>
<span class="fragment">hello
bramus$</span></code></pre></div>
</li>
<li class="fragment">
Run <code>php [-f] <em>file.php</em></code> to “execute” a file that contains PHP code
<ul class="fragment">
<li>You must wrap your PHP code between <code><?php</code> and <code>?></code></li>
<li>The closing <code>?></code> is optional.</li>
</ul>
<div class="fragment"><pre class="bigger"><code class="language-php"><?php
echo 'hello' . PHP_EOL;
?></code></pre></div>
<div class="fragment"><pre class="bigger"><code class="language-php">bramus$<span class="fragment"> php cli-example.php</span>
<span class="fragment">hello
bramus$</span></code></pre></div>
</li>
</ul>
</li>
</section>
<section>
<h2>Running PHP (3)</h2>
<ul>
<li class="fragment">
In a web context
<ul>
<li class="fragment">Give your file a <code>.php</code> extension and the PHP interpreter will interpret the file
<ul>
<li>The webserver must of course be configured to do so</li>
</ul>
</li>
<li class="fragment">PHP 5.4+ ships with a built-in webserver which can be started from anywhere
<pre class="bigger"><code>php -S localhost:8080</code></pre>
<ul>
<li>All PHP files in that folder will be served</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
</section>
<!-- PHP Syntax: Variables/Arrays/Casting, -->
<section>
<section>
<h2>PHP Variables</h2>
</section>
<section>
<h2>Before we dive in</h2>
<ul>
<li class="fragment">
Strings are enclosed by <code>'</code> or <code>"</code>
</li>
<li class="fragment">
Use <code>echo</code> or <code>print</code> to output something onscreen.
<pre class="bigger"><code class="language-php">bramus$<span class="fragment"> php -a</span>
<span class="fragment">Interactive shell
php ></span><span class="fragment"> echo 'hello';
<span class="fragment">hello
php ></span><span class="fragment"> echo('hello');
<span class="fragment">hello
php ></span><span class="fragment"> print 'hello';
<span class="fragment">hello
php ></span><span class="fragment"> print('hello');
<span class="fragment">hello</code></pre>
<ul>
<li class="fragment">The first example is the preferred syntax</li>
</ul>
</li>
<li class="fragment">
Statements end in a <code>;</code>
<pre class="bigger"><code class="language-php">php ><span class="fragment"> echo 'hello'</span><span class="fragment">
php ></span><span class="fragment"> ;</span><span class="fragment">
hello</span></code></pre>
</li>
</ul>
</section>
<section>
<h2>Name, Value & Type</h2>
<ul>
<li class="fragment">
Variables have a <em>name</em> and a <em>value</em>
<ul class="fragment">
<li>Variable names start with a <code>$</code></li>
</ul>
<div class="fragment"><pre class="bigger"><code class="language-php">php ><span class="fragment"> $a = 3;</span><span class="fragment">
php ></span><span class="fragment"> echo $a;</span><span class="fragment">
3</span></code></pre></div>
</li>
<li class="fragment">
Variables are of a certain type
<ul>
<li>Primitive types: boolean, integer, float <em>(aka double)</em>, string</li>
<li>Combined types: Array, Object</li>
<li>Special types: resource, NULL</li>
<li>Pseudo types: mixed, number, callback</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Dynamic & Weak typing</h2>
<ul>
<li class="fragment">PHP is dynamically typed
<ul>
<li>When declaring a variable, you cannot specify the type. PHP will <em>guess</em> the data type when assigning a value</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em">PHP is weakly typed
<ul>
<li>PHP will adjust the type of a var when a new value is assigned
<ul>
<li>This phenomenon is known as <em>implicit type conversion</em> or <em>coercion</em></li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="fragment" style="margin-top: 1em"><pre class="bigger"><code class="language-php">php ><span class="fragment"> $a = 3;</span><span class="fragment">
php ></span><span class="fragment"> echo gettype($a);</span><span class="fragment">
integer
php ></span><span class="fragment"> $a = 'Hello';</span><span class="fragment">
php ></span><span class="fragment"> echo gettype($a);</span><span class="fragment">
string</span></code></pre></div>
<footer class="fragment"><em>Note: Dynamic and weak typing are typical properties of scripting languages</em></footer>
</section>
<section>
<h2>Type casting</h2>
<ul>
<li class="fragment">
To enforce a variable being of a certain type, you must explicitly cast it to that type
<div class="fragment"><pre class="bigger"><code class="language-php">php ><span class="fragment"> $a = 3;</span><span class="fragment">
php ></span><span class="fragment"> echo gettype($a);</span><span class="fragment">
integer
php ></span><span class="fragment"> $a = (string) $a;</span><span class="fragment">
php ></span><span class="fragment"> echo gettype($a);</span><span class="fragment">
string</span></code></pre></div>
</section>
<section>
<h2>A few pointers</h2>
<ul>
<li class="fragment">
PHP is case sensitive
<ul>
<li>
<code>$firstName</code> and <code>$firstname</code> are not the same!
<div class="fragment"><pre class="bigger"><code class="language-php">php ><span class="fragment"> $firstName = 'Bramus';</span><span class="fragment">
php ></span><span class="fragment"> echo $firstName;</span><span class="fragment">
Bramus
php ></span><span class="fragment"> echo $firstname;</span><span class="fragment">
Notice: Undefined variable: firstname in php shell code on line 1</span></code></pre></div>
</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em">
To concatenate strings, you must use a <code>.</code>
<ul>
<li>
When using a <code>+</code>, PHP will think you're adding numbers <em>(weak typing!)</em>
<div class="fragment"><pre class="bigger"><code class="language-php">php ><span class="fragment"> $firstName = 'Bramus';</span><span class="fragment">
php ></span><span class="fragment"> $lastName = 'Van Damme';</span><span class="fragment">
php ></span><span class="fragment"> echo $firstName + ' ' + $lastName;</span><span class="fragment">
0
php ></span><span class="fragment"> echo $firstName . ' ' . $lastName;</span><span class="fragment">
Bramus Van Damme</span>
</code></pre></div>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Interpolation</h2>
<ul>
<li class="fragment">
Also known as <em>Variable Substitution</em>
</li>
<li class="fragment">
When enclosed between <code>"</code>, PHP will try to substitute any string that could resemble a variable
<div class="fragment"><pre class="bigger"><code class="language-php">php > $firstName = 'Bramus';
php > echo "variable: $firstName\n";
variable: Bramus
php > echo "variable: \$firstName\n";
variable: $firstName
php > echo 'variable: $firstName\n';
variable: $firstName\n
php > echo 'variable: \$firstName\n';
variable: \$firstName\n
php > echo 'variable: ' . $firstName . '\n';
variable: Bramus\n
php > echo 'variable: ' . $firstName . "\n";
variable: Bramus
php > echo 'variable: ' . $firstName . PHP_EOL;
variable: Bramus</code></pre></div>
<ul>
<li class="fragment">Course convention: always use <code>'</code> to prevent interpolation</li>
<li class="fragment">Course convention: always use <code>PHP_EOL</code> instead of <code>"\n"</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Arrays (1)</h2>
<div class="fragment"><pre class="bigger"><code class="language-php">php > $arr = array();
php > $arr[] = 'se7en';
php > $arr[] = 123;
php > $arr[] = true;
php > echo $arr[1];
123</code></pre></div>
<ul class="fragment">
<li>Declaration rule isn't mandatory but attests a good coding style</li>
<li>Mixing of types forms no problem</li>
<li>Automatic indexing</li>
<li>Index is numeric and sequential. We call this type of array a <em>sequential array</em></li>
</ul>
</section>
<section>
<h2>Arrays (2)</h2>
<ul class="fragment">
<li>It's possible to set the start index</li>
</ul>
<div class="fragment"><pre class="bigger"><code class="language-php">php > $arr = array();
php > $arr[2] = 'se7en';
php > $arr[] = 123;
php > $arr[] = true;
php > echo $arr[3];
123</code></pre></div>
</section>
<section>
<h2>Arrays (3)</h2>
<ul>
<li class="fragment">
It's possible to set the indices/keys yourself
<div class="fragment"><pre class="bigger"><code class="language-php">php > $arr = array();
php > $arr[2] = 'se7en';
php > $arr[8] = 123;
php > $arr[4] = true;
php > echo $arr[8];
123</code></pre></div>
</li>
<li class="fragment" style="margin-top: 1em">
Indices/keys can be strings too
<div class="fragment"><pre class="bigger"><code class="language-php">php > $arr = array();
php > $arr['BE'] = 'Belgium';
php > $arr['NL'] = 'The Netherlands';
php > $arr['FR'] = 'France';
php > echo $arr['BE'];
Belgium</code></pre></div>
</li>
<li class="fragment" style="margin-top: 1em">We call these <em>associative array</em>s</li>
</ul>
</section>
<section>
<h2>Arrays (4)</h2>
<ul>
<li class="fragment">
It's possible to declare and initialize an array in one go
<div><pre class="bigger"><code class="language-php">php > $arr1 = array('Brussels', 'Ghent', 'Antwerp');</code></pre></div>
<div><pre class="bigger"><code class="language-php">php > $arr2 = array('NL' => 'The Netherlands', 'BE' => 'Belgium');</code></pre></div>
<div class="fragment"><pre class="bigger"><code class="language-php">php > $arr2 = array(
php ( 'NL' => 'The Netherlands',
php ( 'BE' => 'Belgium'
php ( );</code></pre></div>
</li>
<li class="fragment" style="margin-top: 1em">
Since PHP 5.4 this is also possible
<div><pre class="bigger"><code class="language-php">php > $arr1 = ['Brussels', 'Ghent', 'Antwerp'];</code></pre></div>
</li>
</ul>
</section>
</section>
<!-- Code Comments -->
<section>
<section>
<h2>Code Comments</h2>
</section>
<section>
<h2>Code Comments</h2>
<ul>
<li class="fragment">
Same as in Java
<div class="fragment"><pre class="bigger"><code class="language-php">// Single line comment
/* Multi
line
comment
*/
/**
* Preferred way
* to layout
* multi-line
* comments
*/</code></pre></div>
</li>
</ul>
</section>
</section>
<!-- Selections/Iterations/Operators -->
<section>
<section>
<h2>Selections, Iterations & Operators</h2>
</section>
<section>
<h2>Selections (1)</h2>
<ul>
<li class="fragment">
Same as in Java
<div class="fragment"><pre class="bigger"><code class="language-php"><?php
if ($condition) {
// do something
}
if ($condition) {
// do something
} else {
// do something else
}
switch ($var) {
case $value1:
// do something
break;
case $value2:
// do something
break;
default:
// do something
}</code></pre></div>
</li>
</ul>
</section>
<section>
<h2>Selections (2)</h2>
<ul>
<li class="fragment">
Example
<div class="fragment"><pre class="bigger"><code class="language-php"><?php
$a = 7;
if ($a < 9) {
echo '$a is less than 9';
} else {
echo '$a is greater than 9';
}</code></pre></div>
<ul>
<li class="fragment">Course convention: always place curly brackets</li>
<li class="fragment">Course convention: place the curly brackets on the same line</li>
<li class="fragment">Course convention: indent one <code>TAB</code> inside curly brackets</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Selections (3)</h2>
<ul>
<li class="fragment">
When possible, use the ternary operator
<div class="fragment"><pre class="bigger"><code class="language-php"><?php
$a = 7;
echo (($a < 9) ? '$a is less than 9' : '$a is greater than 9');</code></pre></div>
</li>
<li class="fragment">
Since PHP 5.3, the ternary operator has gotten smarter:
<div class="fragment"><pre class="bigger"><code class="language-php"><?php
echo $nickName ?: $firstName;</code></pre></div>
<ul class="fragment">
<li>If <code>$nickName</code> is <em>truthy</em> then it will be outputted. If not <code>$firstName</code> will.</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Iterations (1)</h2>
<ul>
<li class="fragment">
Again, same as in Java
<div class="fragment"><pre class="bigger"><code class="language-php"><?php
while ($condition) {
// do something
}
do {
// do something
} while ($condition);
for ($x = 0; $x < 5; $x++) {
// do something
}
foreach ($arr as $value) {
// do something
}
foreach ($arr as $key => $value) {
// do something
}</code></pre></div>
<ul>
<li class="fragment">Also possible to use <code>break;</code> and <code>continue;</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Iterations (2)</h2>
<ul>
<li class="fragment">
Example
<div class="fragment"><pre class="bigger"><code class="language-php">php ><span class="fragment"> for ($x = 1; $x <= 6; $x++) {</span><span class="fragment">
php { </span><span class="fragment">echo 'Hello 2ICT' . $x . PHP_EOL;</span><span class="fragment">
php { </span><span class="fragment">}</span><span class="fragment">
Hello 2ICT1
Hello 2ICT2
Hello 2ICT3
Hello 2ICT4
Hello 2ICT5
Hello 2ICT6</span></code></pre></div>
</li>
<li class="fragment">
Example
<div class="fragment"><pre class="bigger"><code class="language-php">php > <span class="fragment">$countries = array('BE' => 'Belgium', 'NL' => 'The Netherlands');</span><span class="fragment">
php > </span><span class="fragment">foreach ($countries as $key => $value) {</span><span class="fragment">
php { </span><span class="fragment">echo $key . ' = ' . $value . PHP_EOL;</span><span class="fragment">
php { </span><span class="fragment">}</span><span class="fragment">
BE = Belgium
NL = The Netherlands</span></code></pre></div>
</li>
</ul>
</section>
<section>
<h2>Operators</h2>
<ul>
<li class="fragment">
Same as in Java
<ul>
<li>Relational operators: <code>==</code>, <code>!=</code>, <code><</code>, <code>></code>, <code><=</code>, <code>>=</code></li>
<li>Logical operators: <code>&&</code>, <code>||</code>, <code>!</code>, <code>&</code>, <code>|</code></li>
<li>Post/pre- in/de-crement: <code>$var--</code>, <code>$var++</code>, <code>--$var</code>, <code>++$var</code></li>
<li>Math shorthands: <code>+=</code>, <code>*=</code>, …</li>
</ul>
</li>
<li class="fragment">
Since PHP4 support for identical operators: check for both type and value to be equal
<div><pre class="bigger"><code class="language-php">php > $a = 13; // integer
php > $b = 13.0; // double
php > echo ($a == $b) ? 'equal' : 'not equal';
equal
php > echo ($a === $b) ? 'equal' : 'not equal';
not equal</code></pre></div>
</li>
</ul>
</section>
</section>
<!-- Functions: Built-in PHP Functions, Roll your own functions -->
<section>
<section>
<h2>PHP Functions</h2>
</section>
<section>
<h2>Built-in Functions (1)</h2>
<ul>
<li class="fragment">PHP has a lot of functions built-in</li>
<li class="fragment">
A few string-related functions
<ul>
<li><code>addslashes</code>/<code>stripslashes</code> — <em>see 04.forms</em></li>
<li><code>ltrim</code>/<code>rtrim</code>/<code>trim</code> — strips leading/trailing/enclosing whitespace</li>
<li><code>str_replace</code> — replaces a part of a string with an other value</li>
<li><code>strip_tags</code> — strips html from a string</li>
<li><code>strlen</code> — returns the length of a string</li>
<li><code>strtolower</code>/<code>strtoupper</code> — convert a string to uppercase/lowercase</li>
<li><code>substr</code> — extract a part of a string</li>
<li>...</li>
</ul>
</li>
</ul>
<footer class="fragment">How to use: see <code>http://php.net/<em>nameofthefunction</em></code></footer>
</section>
<section>
<h2>Built-in Functions (2)</h2>
<ul>
<li class="fragment">
A few math-related functions
<ul>
<li><code>ceil</code> — round numerical values up</li>
<li><code>floor</code> — round numerical values down</li>
<li><code>round</code> — round numerical values mathematically</li>
<li><code>rand</code>/<code>mt_rand</code> — generate a random number</li>
<li><code>sqrt</code> — calculate the square root of a number</li>
<li>...</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Built-in Functions (3)</h2>
<ul>
<li class="fragment">
A few array-related functions
<ul>
<li><code>sizeof</code>/<code>count</code> — get the size of an array</li>
<li><code>sort</code> — sort an array</li>
<li><code>asort</code> — sort an associative array</li>
<li><code>explode</code> — split a string on a given delimiter and get an array</li>
<li><code>implode</code> — opposite of <code>explode</code></li>
<li><code>array_push</code> — push an element onto an array</li>
<li><code>array_pop</code> — get an element off the end of an array</li>
<li><code>array_key_exists</code> — check if a key in an associative array exists</li>
<li>...</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Custom functions (1)</h2>
<ul>
<li class="fragment">
Same as in Java
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/functions-chopstringend1.php"><?php
// Our chopStringEnd function
function chopStringEnd($str, $len, $ending) {
if (strlen($str) < $len) {
return $str;
} else {
return substr($str, 0, $len - strlen($str)) . $ending;
}
}
// Our variables
$origStr = '/Users/bramus/Dropbox/Kaho/Lesactiviteiten/Webscripting1';
$cutoffLength = 40;
$endStr = '...';
// Go!
echo chopStringEnd($origStr, $cutoffLength, $endStr);</code></pre></div>
</li>
</ul>
</section>
<section>
<h2>Custom functions (2)</h2>
<ul>
<li class="fragment">
Possible to define default values for function arguments
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/functions-chopstringend2.php"><?php
// Our chopStringEnd function
function chopStringEnd($str, $len = 30, $ending = '---') {
if (strlen($str) < $len) {
return $str;
} else {
return substr($str, 0, $len - strlen($str)) . $ending;
}
}
// Our variables
$origStr = '/Users/bramus/Dropbox/Kaho/Lesactiviteiten/Webscripting1';
$cutoffLength = 40;
$endStr = '...';
// Go!
echo chopStringEnd($origStr, $cutoffLength, $endStr) . PHP_EOL;
echo chopStringEnd($origStr, $cutoffLength) . PHP_EOL;
echo chopStringEnd($origStr);</code></pre></div>
</li>
</ul>
</section>
<section>
<h2>Custom functions (3)</h2>
<ul>
<li class="fragment">
Provide <a href="http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.pkg.html">PHPDoc</a>-style comments
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/functions-chopstringend3.php"><?php
/**
* Chops a string at the given length with the given ending
*
* @param string $str The initial String
* @param int $len The length to chop off at
* @param string $ending The ending to add
*
* @return string The circumcised up string
*/
function chopStringEnd($str, $len = 30, $ending = '---') {
if (strlen($str) < $len) {
return $str;
} else {
return substr($str, 0, $len - strlen($str)) . $ending;
}
}
...</code></pre></div>
</li>
</ul>
<footer class="fragment">This way your IDE will tell you which params are needed, what type they need to be, etc.</footer>
</section>
</section>
<!-- Scoping: Problem, global keywoard, define() function -->
<section>
<section>
<h2>Scoping</h2>
</section>
<section>
<h2>Problem</h2>
<ul>
<li class="fragment">
This won't work
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/scoping1.php"><?php
$host = 'http://www.myhost.com/';
function absUrl($relUrl) {
return $host.$relUrl;
}
echo absUrl('files/uploads/me.jpg');</code></pre></div>
</li>
<li class="fragment">
Result
<div class="fragment"><pre class="bigger"><code class="language-php">Notice: Undefined variable: host in /Users/bramus/Dropbox/Kaho/Lesactiviteiten/.../assets/01/examples/scoping1.php on line 8
files/uploads/me.jpg</code></pre></div>
</li>
</ul>
</section>
<section>
<h2>How we would've fixed this in 1995</h2>
<ul>
<li class="fragment">
The <code>global</code> keyword to the rescue
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/scoping2.php"><?php
$host = 'http://www.myhost.com/';
function absUrl($relUrl) {
global $host;
return $host . $relUrl;
}
echo absUrl('files/uploads/me.jpg');</code></pre></div>
</li>
<li class="fragment">
Don't ever do this; it's a bad programming style!
</li>
</ul>
</section>
<section>
<h2>How we would've fixed this in 1998</h2>
<ul>
<li class="fragment">
Extra function argument
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/scoping3.php"><?php
$host = 'http://www.myhost.com/';
function absUrl($relUrl, $host) {
return $host . $relUrl;
}
echo absUrl('files/uploads/me.jpg', $host);</code></pre></div>
</li>
<li class="fragment">
Extra function argument, revised
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/scoping4.php"><?php
function absUrl($relUrl, $host = 'http://www.myhost.com/') {
return $host . $relUrl;
}
echo absUrl('files/uploads/me.jpg');</code></pre></div>
</li>
</ul>
</section>
<section>
<h2>How to properly fix it (since 2000)</h2>
<ul>
<li class="fragment">
Use <code>define()</code>, a function that shipped with PHP4
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/scoping5.php"><?php
define('HOST', 'http://www.myhost.com/');
function absUrl($relUrl) {
return HOST . $relUrl;
}
echo absUrl('files/uploads/me.jpg');</code></pre></div>
</li>
<li class="fragment">
Course convention: always use <code>define()</code> to define global constants, never use the <code>global</code> keyword
</li>
</ul>
</section>
<section>
<h2>How we can fix this since 2009</h2>
<ul>
<li class="fragment">
Since PHP 5.3 it's possible to use the <code>use</code> keyword to inject a variable into a function
<div class="fragment"><pre class="bigger"><code class="language-php" data-overlay-example="assets/01/examples/scoping6.php"><?php
$host = 'http://www.myhost.com/';
$absUrl = function ($relUrl) use ($host) {
return $host . $relUrl;
};
echo $absUrl('files/uploads/me.jpg');</code></pre></div>
</li>
<li class="fragment" style="margin-top: 1em">
This syntax is mostly used when working with callback functions. We'll stick to using <code>define()</code> for now.
</li>
</ul>
<footer class="fragment">Note: alternative function definition syntax required!</footer>
</section>
</section>
<!-- Errors and debugging: @, dump() -->
<section>
<section>
<h2>Errors and debugging</h2>
</section>