Table of Contents

Open all
Close all
Preface
25
Materials for the Book
25
The Concept
26
The Content
28
About the PHP 8.3 Edition
29
Support
30
PART I Preparations
31
1 Introduction to PHP
33
1.1 History of PHP
33
1.2 Success and Commitment
34
1.3 The Concept of PHP
36
1.4 The Most Important New Features in PHP 8 to 8.4
37
1.5 The Most Important Features in PHP 7.3
41
1.6 The Most Important Features in PHP 7
42
1.7 The Most Important Features in PHP 5.4, 5.5, and 5.6
43
1.8 Downloads and Documentation
44
2 Installation
45
2.1 Install PHP
45
2.1.1 Structure of PHP
47
2.1.2 Windows
48
2.1.3 macOS
62
2.1.4 Linux
67
2.2 PEAR
75
2.2.1 Install PEAR
75
2.2.2 PEAR Packages without Installation
78
3 Testing and Help
79
3.1 Common Errors
79
3.1.1 The Page Cannot Be Displayed
79
3.1.2 The Website Was Not Found—File Not Found
80
3.1.3 Server Not Found
81
3.1.4 Unable to Initialize Module
83
3.1.5 Module Not Found
83
3.1.6 The Browser Opens a Download Window
84
3.1.7 No Input File Specified (or Something Similar)
85
3.1.8 Call to Undefined Function
86
3.1.9 Internal Server Error
86
3.1.10 VCRUNTIME140.DLL Is Missing
87
3.1.11 White Page in the Browser
88
3.1.12 Extension Does Not Appear in phpinfo()
88
3.2 Additional Resources
88
PART II Getting Started with PHP
91
4 PHP Language Basics
93
4.1 PHP in HTML
93
4.1.1 Comments
94
4.1.2 Instructions
95
4.1.3 External File
96
4.2 Output with PHP
99
4.2.1 Abstract
100
4.2.2 Quotation Marks
100
4.3 Variables
101
4.3.1 Data Types
101
4.3.2 Naming
104
4.3.3 Variable Variables
105
4.3.4 Output Variables
105
4.3.5 Useful and Helpful Information
107
4.3.6 Predefined Variables
111
4.4 Constants
112
5 Programming
113
5.1 Operators
113
5.1.1 Arithmetic Operators
113
5.1.2 Comparison Operators
117
5.1.3 Logical Operators
123
5.1.4 Binary Numbers and Bitwise Operators
124
5.1.5 Operators that Step out of Line
128
5.1.6 Ranking of the Operators
134
5.2 Case Distinctions
135
5.2.1 "if"
136
5.2.2 "switch"
142
5.2.3 "match"
146
5.3 Loops
149
5.3.1 "for"
149
5.3.2 "while"
153
5.3.3 "do-while"
159
5.4 Jumps
161
6 Functions and Language Constructs
163
6.1 Functions
163
6.1.1 Parameters
164
6.1.2 Validity of Variables
169
6.1.3 Return Value
171
6.1.4 Function Names in Variables
174
6.1.5 Anonymous Functions
174
6.1.6 Recursive Functions
176
6.1.7 Type Declarations
178
6.1.8 Enumerations
185
6.1.9 Helpful and Useful Information
187
6.1.10 Functions of PHP
190
6.2 Language Constructs
192
6.2.1 "heredoc"
193
6.2.2 "nowdoc"
195
6.2.3 Formatted Strings
196
6.2.4 "print_r" and "var_dump"
198
7 Strings
201
7.1 Connect
201
7.2 Splitting and Joining
202
7.2.1 Accessing Characters
202
7.2.2 Interrupt Evenly
202
7.2.3 Line Breaks
203
7.2.4 Split into Strings
205
7.2.5 Strings and Arrays
207
7.3 Upper- and Lowercase
212
7.4 Pruning
214
7.4.1 Cut Out Characters
214
7.4.2 Remove Whitespaces
215
7.5 Search and Replace
216
7.5.1 Search
216
7.5.2 Replace
221
7.6 Special Characters, HTML, and the Like
223
7.6.1 Escaping for Databases
223
7.6.2 Escaping for Regular Expressions
224
7.6.3 HTML
225
7.6.4 URLs
229
7.7 Compare
231
7.7.1 Comparison Functions
231
7.7.2 Similarities and Differences
231
7.7.3 Pronunciation
232
7.8 Helpful and Useful Information
233
7.8.1 ASCII and Conversion
233
7.8.2 Unicode Code Point
233
7.8.3 Encryption
234
7.8.4 Turn Over
236
7.8.5 Multibyte String Functions
236
8 Arrays
239
8.1 Basics
239
8.1.1 Create Arrays
239
8.1.2 Adding and Changing Elements
241
8.1.3 Delete Elements
242
8.1.4 Associative Arrays
242
8.1.5 Shorthand with JSON
243
8.1.6 Multidimensional Arrays
243
8.2 Arrays and Loops
244
8.2.1 "for"
244
8.2.2 "foreach"
245
8.2.3 Functions for Iteration
248
8.3 Examine
250
8.4 Transform
251
8.4.1 Adding and Removing
251
8.4.2 Delete and Replace
252
8.4.3 Connect
254
8.4.4 Variables and Arrays
256
8.4.5 Dereferencing
257
8.4.6 Conversion to Parameters
258
8.5 Search and Sort
259
8.5.1 Search
260
8.5.2 Sort
260
8.6 Superglobal Arrays
261
9 Mathematical and Date Functions
267
9.1 Math
267
9.1.1 Basics
267
9.1.2 Constants
268
9.1.3 Convert Numbers
269
9.1.4 Random Numbers
272
9.1.5 Maximum, Minimum, and Rounding
274
9.1.6 Radians and More
276
9.1.7 Higher Accuracy
276
9.2 Date and Time
278
9.2.1 Current Date with Functions
278
9.2.2 Current Date with "DateTime"
283
9.2.3 Any Date Values
284
9.2.4 Timestamp
285
9.2.5 Format Date
289
9.2.6 Countdown: Calculate with Dates
291
10 Regular Expressions
295
10.1 Basics
295
10.2 Functions for Regular Expressions
297
10.2.1 "preg_match()"
297
10.2.2 Further Functions
301
10.2.3 Reuse Replacement Strings
302
10.2.4 Modifiers for Search Patterns
302
10.3 Application Examples
303
10.3.1 Zip Codes
303
10.3.2 Telephone and Fax Numbers
304
10.3.3 Filter Links
304
11 Object-Oriented Programming
307
11.1 History of Object Orientation in PHP
307
11.2 Classes and Objects: Basic Concepts
309
11.2.1 Classes and Objects in PHP
310
11.2.2 Properties
311
11.2.3 Methods
313
11.2.4 Inheritance
317
11.3 Advanced
320
11.3.1 Clone Objects
320
11.3.2 Constructor
321
11.3.3 Destructor
323
11.3.4 Private, Protected, and So On
324
11.3.5 Interfaces
331
11.3.6 Late Static Binding
333
11.3.7 Abstract Classes
335
11.3.8 Anonymous Classes
336
11.3.9 Constants
338
11.3.10 Overloaded
339
11.3.11 Traits
345
11.4 Helpful and Useful Information
348
11.4.1 Error Level
348
11.4.2 "spl_autoload_register()"
350
11.4.3 "__METHOD__"
351
11.4.4 "__toString()"
352
11.4.5 Class Types and "instanceof"
353
11.4.6 Compare Objects
355
11.4.7 Serialize Objects
356
11.4.8 Automated Reading of Objects
359
11.4.9 Iteration
360
11.4.10 Reflection API
362
11.4.11 SPL
363
11.4.12 Native Metadata with Attributes
372
11.5 Namespaces
376
11.5.1 Why Namespaces?
377
11.5.2 Working with Namespaces
378
11.5.3 Use Namespaces
379
11.5.4 Determine the Current Namespace
380
11.5.5 Namespaces via Alias
380
12 Design Patterns: MVC and Co.
385
12.1 Laminas
386
12.2 MVC
387
12.3 Adapter and Factory
394
PART III Web Techniques
399
13 Forms
401
13.1 Preparations
401
13.2 Forms with PHP
404
13.2.1 The “Good” Old Days
405
13.2.2 HTTP Methods
408
13.2.3 Text Field(s)
410
13.2.4 Radio Buttons
412
13.2.5 Checkboxes
413
13.2.6 Selection Lists
415
13.2.7 Determine the HTTP Method
418
13.3 Form Validation
421
13.3.1 Text Field(s)
421
13.3.2 Radio Buttons
423
13.3.3 Checkboxes
424
13.3.4 Selection Lists
425
13.3.5 More Detailed Error Messages
430
13.4 Prefill
434
13.4.1 Preparations
435
13.4.2 Text Fields
435
13.4.3 Radio Buttons
436
13.4.4 Checkboxes
437
13.4.5 Selection Lists
437
13.5 File Uploads
444
13.6 Application Examples
448
13.6.1 JavaScript Form Check
448
13.6.2 Picture Gallery
453
13.7 Settings
455
14 Cookies
457
14.1 Preparations
457
14.2 Facts and Background
459
14.2.1 What Is a Cookie?
459
14.2.2 Restrictions
460
14.2.3 The Transparent Surfer?
460
14.3 Working with Cookies in PHP
461
14.3.1 Set Cookies
461
14.3.2 Read Cookies
469
14.3.3 Delete Cookies
471
14.3.4 "New" Cookies
474
14.4 Cookie Test
476
14.5 Final Considerations
479
15 Sessions
481
15.1 Preparations
481
15.2 Facts, Background, and Configuration
482
15.2.1 Keep Data
483
15.2.2 Performance
485
15.3 Working with Sessions in PHP
486
15.3.1 Write Data
486
15.3.2 Read Out Data
488
15.3.3 Delete Data
490
15.3.4 Configure Sessions
491
15.4 Protected Area
491
15.5 Sessions in Databases
493
15.6 Security Concerns
501
15.6.1 No Sessions without Cookies!
501
15.6.2 Check the Referrer
503
15.6.3 Change the Session ID
503
15.6.4 Making Cross-Site Request Forgery More Difficult
503
15.6.5 Prevent JavaScript Access to the Session Cookie
504
16 Email
505
16.1 Preparations
505
16.2 Sending Mails with PHP
510
16.2.1 Standard Mails
510
16.2.2 MIME Mails
514
16.2.3 IMAP and POP
521
PART IV Databases
523
17 SQL
525
17.1 Create Databases and Tables
526
17.1.1 Primary Key
526
17.1.2 Data Types
527
17.2 Enter Data
529
17.3 Query Data
530
17.4 Update Data
532
17.5 Delete Data
533
17.6 Special Features
534
17.6.1 Relational Database Design
534
17.6.2 Joins
536
17.6.3 Aggregate Functions
536
17.6.4 Transactions
538
17.6.5 Stored Procedures
539
18 PDO
541
18.1 Preparations
542
18.2 Database Access with PDO
544
18.2.1 Connection Setup
544
18.2.2 Queries
545
18.2.3 Return Values
548
18.2.4 Special Features
549
18.3 Guestbook
550
18.3.1 Create Table
550
18.3.2 Enter Data
551
18.3.3 Output Data
553
18.3.4 Delete Data
554
18.3.5 Edit Data
556
19 MySQL
559
19.1 Preparations
559
19.2 Database Access with MySQL
566
19.2.1 Connection Setup
566
19.2.2 Queries
568
19.2.3 Return Values
572
19.2.4 Special Features
576
19.3 Application Example
588
19.3.1 Create Table
588
19.3.2 Enter Data
588
19.3.3 Output Data
591
19.3.4 Delete Data
592
19.3.5 Edit Data
594
19.4 Settings
596
20 SQLite
599
20.1 Preparations
599
20.2 Database Access with SQLite
601
20.2.1 Connection Setup
601
20.2.2 Queries
602
20.2.3 Return Values
605
20.2.4 Special Features
608
20.2.5 Migration of Old Code
611
20.3 Application Example
613
20.3.1 Create Table
613
20.3.2 Enter Data
613
20.3.3 Output Data
615
20.3.4 Delete Data
616
20.3.5 Edit Data
618
21 Microsoft SQL Server
621
21.1 Preparations
621
21.2 Microsoft SQL Server Driver for PHP
623
21.2.1 Connection Setup
625
21.2.2 Queries
625
21.2.3 Return Values
626
21.2.4 Special Features
628
21.3 Application Example
632
21.3.1 Create Table
632
21.3.2 Enter Data
633
21.3.3 Output Data
634
21.3.4 Delete Data
636
21.3.5 Edit Data
637
22 Oracle
641
22.1 Preparations
641
22.2 Database Access with Oracle
645
22.2.1 Connection Setup
645
22.2.2 Queries
646
22.2.3 Return Values
649
22.2.4 Special Features
652
22.3 Application Example
654
22.3.1 Create Table
654
22.3.2 Enter Data
655
22.3.3 Output Data
656
22.3.4 Delete Data
657
22.3.5 Edit Data
659
23 PostgreSQL
661
23.1 Preparations
661
23.2 Database Access with PostgreSQL
664
23.2.1 Connection Setup
665
23.2.2 Queries
665
23.2.3 Return Values
668
23.2.4 Special Features
670
23.3 Application Example
676
23.3.1 Create Table
676
23.3.2 Enter Data
677
23.3.3 Output Data
679
23.3.4 Delete Data
680
23.3.5 Edit Data
681
23.4 Settings
683
24 MongoDB
685
24.1 Preparations
685
24.2 Database Access with MongoDB
690
24.2.1 Connection Setup
691
24.2.2 Insert
692
24.2.3 Queries and Return Values
693
24.2.4 Refresh
695
24.2.5 Delete
697
24.3 Application Example
697
24.3.1 Create Data Memory
697
24.3.2 Enter Data
698
24.3.3 Output Data
699
24.3.4 Delete Data
700
24.3.5 Edit Data
702
24.4 Settings
704
PART V Communication
705
25 Files
707
25.1 Preparations
707
25.2 File Handling with PHP
708
25.2.1 Working with Files
708
25.2.2 Working with the File System
714
25.3 Application Examples
718
25.3.1 Guestbook
718
25.3.2 File Browser
723
25.4 Settings
725
26 HTTP and Beyond: Connections to the Outside World
727
26.1 Preparations
727
26.2 External Connection with PHP
727
26.2.1 Streams
728
26.2.2 HTTP Streams
729
26.2.3 PHP Streams
735
26.2.4 Compression Streams
738
26.3 Fibers
741
26.4 Application Examples
742
26.4.1 Text Versions of Web Pages
742
26.4.2 Online Compressor
744
27 APIs and Services
747
27.1 Preparation
747
27.1.1 Web Services Basics
747
27.1.2 Installation
752
27.2 REST
754
27.3 nuSOAP
758
27.3.1 Server
758
27.3.2 Client
760
27.3.3 WSDL
761
27.3.4 Conclusion
765
27.4 PHP-SOAP
765
27.4.1 Server
765
27.4.2 Client
766
27.4.3 WSDL
768
27.4.4 Conclusion
769
28 JavaScript
771
28.1 Preparations
771
28.2 Connecting JavaScript with PHP
772
28.2.1 Reading PHP Variables with JavaScript
772
28.2.2 Reading JavaScript Variables with PHP
776
28.3 Ajax
777
28.4 WebSockets
782
28.4.1 Server
783
28.4.2 Client
786
PART IV Data Formats
789
29 XML
791
29.1 Preparations
791
29.1.1 XML Basics
791
29.1.2 Installation
796
29.2 XML Access
797
29.2.1 SAX
797
29.2.2 SimpleXML
801
29.2.3 DOM Access
805
29.2.4 Validation
811
29.2.5 XSLT
813
29.3 XMLReader and XMLWriter
814
29.3.1 XMLReader
814
29.3.2 XMLWriter
815
29.4 EXIF
817
29.4.1 Preparation
817
29.4.2 Implementation
818
30 Graphics with PHP
823
30.1 Preparations
823
30.1.1 Installation
823
30.2 GD 2 in Action
824
30.2.1 Basic Framework
824
30.2.2 Text
826
30.2.3 Shapes
829
30.2.4 Lines and Styles
831
30.2.5 Output of the Images
832
30.2.6 Image Processing
832
30.2.7 Dynamic Diagram
837
30.3 The Alternatives
842
30.3.1 ImageMagick
842
30.3.2 GraphicsMagick
843
30.3.3 NetPBM
843
31 PDF with PHP
845
31.1 Preparation
846
31.1.1 TCPDF
846
31.1.2 FPDF
847
31.1.3 Haru
847
31.2 TCPDF
847
31.2.1 Basics
847
31.2.2 Cells
850
31.2.3 Lines and Points
852
31.2.4 Pie Chart
854
31.2.5 Write HTML
857
31.3 FPDF
859
31.3.1 Basics
859
31.3.2 Drawing
860
31.4 Conclusion
861
PART VII Administration and Security
863
32 Security
865
32.1 User Input
866
32.2 XSS
869
32.3 SQL Injection
874
32.4 Hidden Fields
876
32.5 Input Filter
878
32.6 Cross-Site Request Forgery
881
32.7 Screen Scraping and Captchas
886
32.8 Encrypt Passwords
891
33 Authentication
895
33.1 Apache Authentication
896
33.2 IIS Authentication
900
33.3 HTTP Authentication by Hand
901
33.4 Standards-Based Authentication
903
33.4.1 Preparation of the Google Project
905
33.4.2 Access via PHP
910
33.5 Conclusion
915
34 Configuration Options in php.ini
917
34.1 Where to Configure?
917
34.1.1 Storage Location
917
34.1.2 Other Configuration Files
918
34.2 Configure What?
920
PART VIII Beyond PHP
929
35 Troubleshooting with Xdebug
931
35.1 Debugging by Hand
933
35.2 Debugging with Xdebug
935
35.3 Resolution
939
36 Unit Tests with PHPUnit
941
36.1 Unit Tests
941
36.2 Install PHPUnit
942
36.3 Testing with PHPUnit
943
37 Apache Functions
947
37.1 Preparations
947
37.2 Application Examples
948
37.2.1 Information about Apache
948
37.2.2 Reading HTTP Headers
949
37.2.3 URI Information
950
37.2.4 Integrate Other Server Technologies
952
37.2.5 Terminate Apache Process
953
38 Composer
955
38.1 Install Composer
955
38.2 Install Packages via Composer
957
38.3 Customize Your Own Code for Composer
961
39 PHP Extensions
965
39.1 Programming
966
39.2 Compile
971
39.3 Testing
974
40 Contribute to PHP
977
40.1 Patches for PHP
977
40.1.1 Bug Fix
978
40.1.2 New Features
982
40.2 Further Options
983
The Authors
985
Index
987