Copyright....4
Table of Contents....7
Preface....25
Audience....25
Assumptions This Book Makes....25
Organization of This Book....26
Conventions Used in This Book....26
Using Code Examples....27
OReilly Online Learning....28
How to Contact Us....29
Acknowledgments....29
Chapter 1. Introduction to Dynamic Web Content....31
HTTP and HTML: Berners-Lees Basics....32
The RequestResponse Procedure....32
The Benefits of PHP, MySQL, JavaScript, CSS, and HTML5....35
MariaDB: The MySQL Clone....36
Using PHP....36
Using MySQL....37
Using JavaScript....38
Using CSS....40
And Then Theres HTML5....40
The Apache Web Server....41
Handling Mobile Devices....42
About Open Source....42
Bringing It All Together....43
Questions....45
Chapter 2. Setting Up a Development Server....47
What Is a WAMP, MAMP, or LAMP?....48
Installing AMPPS on Windows....48
Testing the Installation....53
Accessing the Document Root (Windows)....55
Alternative WAMPs....56
Installing AMPPS on macOS....57
Accessing the Document Root (macOS)....58
Installing a LAMP on Linux....59
Working Remotely....59
Logging In....60
Using SFTP or FTPS....60
Using a Code Editor....61
Using an IDE....62
Questions....64
Chapter 3. Introduction to PHP....65
Incorporating PHP Within HTML....65
This Books Examples....67
The Structure of PHP....67
Using Comments....67
Basic Syntax....68
Variables....69
Operators....74
Variable Assignment....78
Multiline Commands....80
Variable Typing....82
Constants....83
Predefined Constants....84
The Difference Between the echo and print Commands....85
Functions....85
Variable Scope....86
Questions....92
Chapter 4. Expressions and Control Flow in PHP....93
Expressions....93
TRUE or FALSE?....94
Literals and Variables....95
Operators....96
Operator Precedence....97
Associativity....99
Relational Operators....100
Conditionals....104
The if Statement....105
The else Statement....106
The elseif Statement....108
The switch Statement....109
The ? (or ternary) Operator....112
Looping....113
while Loops....114
do...while Loops....116
for Loops....116
Breaking Out of a Loop....118
The continue Statement....119
Implicit and Explicit Casting....120
PHP Dynamic Linking....121
Dynamic Linking in Action....122
Questions....123
Chapter 5. PHP Functions and Objects....125
PHP Functions....126
Defining a Function....127
Returning a Value....128
Returning an Array....129
Passing Arguments by Reference....130
Returning Global Variables....132
Recap of Variable Scope....132
Including and Requiring Files....133
The include Statement....133
Using include_once....133
Using require and require_once....134
PHP Version Compatibility....134
PHP Objects....135
Terminology....136
Declaring a Class....137
Creating an Object....138
Accessing Objects....138
Cloning Objects....140
Constructors....141
Destructors....141
Writing Methods....142
Declaring Properties....143
Declaring Constants....144
Property and Method Scope....144
Static Methods....145
Static Properties....146
Inheritance....147
Questions....151
Chapter 6. PHP Arrays....153
Basic Access....153
Numerically Indexed Arrays....153
Associative Arrays....155
Assignment Using the array Keyword....156
The foreach...as Loop....157
Multidimensional Arrays....159
Using Array Functions....162
is_array....162
count....163
sort....163
shuffle....163
explode....164
extract....165
compact....165
reset....167
end....167
Questions....167
Chapter 7. Practical PHP....169
Using printf....169
Precision Setting....170
String Padding....172
Using sprintf....173
Date and Time Functions....173
Date Constants....176
Using checkdate....176
File Handling....177
Checking Whether a File Exists....177
Creating a File....177
Reading from Files....179
Copying Files....180
Moving a File....180
Deleting a File....181
Updating Files....181
Locking Files for Multiple Accesses....182
Reading an Entire File....184
Uploading Files....185
System Calls....190
XHTML or HTML5?....192
Questions....193
Chapter 8. Introduction to MySQL....195
MySQL Basics....195
Summary of Database Terms....196
Accessing MySQL via the Command Line....196
Starting the Command-Line Interface....197
Using the Command-Line Interface....201
MySQL Commands....202
Data Types....207
Indexes....217
Creating an Index....217
Querying a MySQL Database....223
Joining Tables Together....233
Using Logical Operators....235
MySQL Functions....236
Accessing MySQL via phpMyAdmin....236
Questions....237
Chapter 9. Mastering MySQL....239
Database Design....239
Primary Keys: The Keys to Relational Databases....240
Normalization....241
First Normal Form....242
Second Normal Form....244
Third Normal Form....246
When Not to Use Normalization....248
Relationships....249
One-to-One....249
One-to-Many....250
Many-to-Many....251
Databases and Anonymity....252
Transactions....253
Transaction Storage Engines....253
Using BEGIN....254
Using COMMIT....255
Using ROLLBACK....255
Using EXPLAIN....256
Backing Up and Restoring....257
Using mysqldump....257
Creating a Backup File....259
Restoring from a Backup File....261
Dumping Data in CSV Format....261
Planning Your Backups....262
Questions....262
Chapter 10. Whats New in PHP 8 and MySQL 8....265
About This Chapter....265
PHP 8....266
Named Parameters....266
Attributes....267
Constructor Properties....267
Just-In-Time Compilation....268
Union Types....268
Null-safe Operator....268
match Expressions....268
New Functions....269
MySQL 8....273
Updates to SQL....273
JSON (JavaScript Object Notation)....274
Geography Support....274
Reliability....274
Speed and Performance....274
Management....275
Security....275
Questions....276
Chapter 11. Accessing MySQL Using PHP....277
Querying a MySQL Database with PHP....277
The Process....277
Creating a Login File....278
Connecting to a MySQL Database....279
A Practical Example....284
The _POST Array....287
Deleting a Record....288
Displaying the Form....288
Querying the Database....289
Running the Program....290
Practical MySQL....291
Creating a Table....292
Describing a Table....293
Dropping a Table....294
Adding Data....294
Retrieving Data....295
Updating Data....296
Deleting Data....297
Using AUTO_INCREMENT....297
Performing Additional Queries....299
Preventing Hacking Attempts....300
Steps You Can Take....301
Using Placeholders....303
Preventing JavaScript Injection into HTML....305
Questions....307
Chapter 12. Form Handling....309
Building Forms....309
Retrieving Submitted Data....311
Default Values....312
Input Types....313
Sanitizing Input....320
An Example Program....322
HTML5 Enhancements....324
The autocomplete Attribute....325
The autofocus Attribute....325
The placeholder Attribute....325
The required Attribute....326
Override Attributes....326
The width and height Attributes....326
The min and max Attributes....326
The step Attribute....326
The form Attribute....327
The list Attribute....327
The color Input Type....327
The number and range Input Types....327
Date and Time Pickers....328
Questions....328
Chapter 13. Cookies, Sessions, and Authentication....329
Using Cookies in PHP....329
Setting a Cookie....331
Accessing a Cookie....332
Destroying a Cookie....332
HTTP Authentication....332
Storing Usernames and Passwords....336
An Example Program....338
Using Sessions....341
Starting a Session....342
Ending a Session....345
Setting a Timeout....346
Session Security....347
Questions....350
Chapter 14. Exploring JavaScript....351
JavaScript and HTML Text....352
Using Scripts Within a Document Head....353
Older and Nonstandard Browsers....354
Including JavaScript Files....354
Debugging JavaScript Errors....355
Using Comments....355
Semicolons....356
Variables....356
String Variables....357
Numeric Variables....357
Arrays....357
Operators....358
Arithmetic Operators....358
Assignment Operators....359
Comparison Operators....359
Logical Operators....360
Incrementing, Decrementing, and Shorthand Assignment....360
String Concatenation....360
Escape Characters....361
Variable Typing....361
Functions....363
Global Variables....363
Local Variables....363
Using let and const....364
The Document Object Model....367
Another Use for the Symbol....369
Using the DOM....370
About document.write....371
Using console.log....371
Using alert....371
Writing into Elements....371
Using document.write....371
Questions....372
Chapter 15. Expressions and Control Flow in JavaScript....373
Expressions....373
Literals and Variables....374
Operators....375
Operator Precedence....376
Associativity....376
Relational Operators....377
The with Statement....380
Using onerror....381
Using try...catch....382
Conditionals....383
The if Statement....383
The else Statement....383
The switch Statement....384
The ? Operator....386
Looping....387
while Loops....387
do...while Loops....387
for Loops....388
Breaking Out of a Loop....389
The continue Statement....389
Explicit Casting....390
Questions....391
Chapter 16. JavaScript Functions, Objects, and Arrays....393
JavaScript Functions....393
Defining a Function....393
Returning a Value....395
Returning an Array....397
JavaScript Objects....398
Declaring a Class....398
Creating an Object....400
Accessing Objects....400
The prototype Keyword....400
JavaScript Arrays....403
Numeric Arrays....404
Associative Arrays....405
Multidimensional Arrays....406
Using Array Methods....407
Questions....412
Chapter 17. JavaScript and PHP Validation and Error Handling....415
Validating User Input with JavaScript....415
The validate.html Document (Part 1)....416
The validate.html Document (Part 2)....418
Regular Expressions....421
Matching Through Metacharacters....422
Fuzzy Character Matching....422
Grouping Through Parentheses....423
Character Classes....424
Indicating a Range....424
Negation....424
Some More Complicated Examples....425
Summary of Metacharacters....428
General Modifiers....429
Using Regular Expressions in JavaScript....430
Using Regular Expressions in PHP....430
Redisplaying a Form After PHP Validation....431
Questions....437
Chapter 18. Using Asynchronous Communication....439
What Is Asynchronous Communication?....440
Using XMLHttpRequest....440
Your First Asynchronous Program....441
Using GET Instead of POST....445
Sending XML Requests....447
Using Frameworks for Asynchronous Communication....451
Questions....451
Chapter 19. Introduction to CSS....453
Importing a Stylesheet....454
Importing CSS from Within HTML....454
Embedded Style Settings....455
Using IDs....455
Using Classes....455
Using Semicolons....456
CSS Rules....456
Multiple Assignments....456
Using Comments....457
Style Types....458
Default Styles....458
User Styles....458
External Stylesheets....459
Internal Styles....459
Inline Styles....460
CSS Selectors....460
The Type Selector....460
The Descendant Selector....460
The Child Selector....461
The ID Selector....462
The Class Selector....463
The Attribute Selector....464
The Universal Selector....464
Selecting by Group....465
The CSS Cascade....466
Stylesheet Creators....466
Stylesheet Methods....466
Stylesheet Selectors....467
The Difference Between div and span Elements....469
Measurements....471
Fonts and Typography....473
font-family....473
font-style....474
font-size....474
font-weight....475
Managing Text Styles....475
Decoration....476
Spacing....476
Alignment....477
Transformation....477
Indenting....477
CSS Colors....478
Short Color Strings....478
Gradients....479
Positioning Elements....480
Absolute Positioning....480
Relative Positioning....481
Fixed Positioning....481
Pseudoclasses....483
Shorthand Rules....485
The Box Model and Layout....486
Setting Margins....486
Applying Borders....488
Adjusting Padding....489
Object Contents....491
Questions....491
Chapter 20. Advanced CSS with CSS3....493
Attribute Selectors....494
Matching Parts of Strings....494
The box-sizing Property....495
CSS3 Backgrounds....496
The background-clip Property....496
The background-origin Property....498
The background-size Property....498
Using the auto Value....499
Multiple Backgrounds....499
CSS3 Borders....501
The border-color Property....501
The border-radius Property....501
Box Shadows....504
Element Overflow....505
Multicolumn Layout....505
Colors and Opacity....507
HSL Colors....507
HSLA Colors....508
RGB Colors....508
RGBA Colors....509
The opacity Property....509
Text Effects....509
The text-shadow Property....509
The text-overflow Property....509
The word-wrap Property....510
Web Fonts....511
Google Web Fonts....512
Transformations....513
3D Transformations....514
Transitions....515
Properties to Transition....515
Transition Duration....516
Transition Delay....516
Transition Timing....516
Shorthand Syntax....517
Questions....518
Chapter 21. Accessing CSS from JavaScript....521
Revisiting the getElementById Function....521
The O Function....521
The S Function....522
The C Function....523
Including the Functions....524
Accessing CSS Properties from JavaScript....524
Some Common Properties....525
Other Properties....526
Inline JavaScript....528
The this Keyword....528
Attaching Events to Objects in a Script....529
Attaching to Other Events....529
Adding New Elements....530
Removing Elements....532
Alternatives to Adding and Removing Elements....532
Using Interrupts....533
Using setTimeout....534
Canceling a Timeout....535
Using setInterval....535
Using Interrupts for Animation....537
Questions....539
Chapter 22. Introduction to jQuery....541
Why jQuery?....542
Including jQuery....542
Choosing the Right Version....543
Downloading....544
Using a Content Delivery Network....544
Customizing jQuery....545
jQuery Syntax....545
A Simple Example....546
Avoiding Library Conflicts....547
Selectors....547
The css Method....547
The Element Selector....548
The ID Selector....549
The Class Selector....549
Combining Selectors....549
Handling Events....550
Waiting Until the Document Is Ready....551
Event Functions and Properties....552
The blur and focus Events....553
The this Keyword....554
The click and dblclick Events....554
The keypress Event....555
Considerate Programming....557
The mousemove Event....557
Other Mouse Events....560
Alternative Mouse Methods....561
The submit Event....562
Special Effects....563
Hiding and Showing....564
The toggle Method....565
Fading In and Out....566
Sliding Elements Up and Down....567
Animations....568
Stopping Animations....571
Manipulating the DOM....572
The Difference Between the text and html Methods....573
The val and attr Methods....573
Adding and Removing Elements....575
Dynamically Applying Classes....577
Modifying Dimensions....577
The width and height Methods....578
The innerWidth and innerHeight Methods....580
The outerWidth and outerHeight Methods....580
DOM Traversal....581
Parent Elements....581
Child Elements....585
Sibling Elements....585
Selecting the Next and Previous Elements....587
Traversing jQuery Selections....589
The is Method....590
Using jQuery Without Selectors....592
The .each Method....592
The .map Method....593
Using Asynchronous Communication....593
Using the POST Method....594
Using the GET Method....594
Plug-ins....595
jQuery User Interface....595
Other Plug-ins....596
Questions....596
Chapter 23. Introduction to jQuery Mobile....599
Including jQuery Mobile....600
Getting Started....601
Linking Pages....603
Linking Synchronously....604
Linking Within a Multipage Document....604
Page Transitions....605
Styling Buttons....610
List Handling....612
Filterable Lists....613
List Dividers....615
What Next?....618
Questions....618
Chapter 24. Introduction to React....621
What Is the Point of React Anyway?....622
Accessing the React Files....623
Including babel.js....624
Our First React Project....625
Using a Function Instead of a Class....626
Pure and Impure Code: A Golden Rule....628
Using Both a Class and a Function....628
Props and Components....629
The Differences Between Using a Class and a Function....630
React State and Life Cycle....631
Using Hooks (if You Use Node.js)....634
Events in React....634
Inline JSX Conditional Statements....636
Using Lists and Keys....637
Unique Keys....638
Handling Forms....639
Using Text Input....640
Using textarea....642
Using select....643
React Native....645
Creating React Native Apps....645
Further Reading....645
Taking React to the Next Level....646
Questions....646
Chapter 25. Introduction to HTML5....649
The Canvas....649
Geolocation....651
Audio and Video....653
Forms....654
Local Storage....654
Web Workers....654
Questions....655
Chapter 26. The HTML5 Canvas....657
Creating and Accessing a Canvas....657
The toDataURL Function....659
Specifying an Image Type....661
The fillRect Method....661
The clearRect Method....661
The strokeRect Method....662
Combining These Commands....662
The createLinearGradient Method....663
The addColorStop Method in Detail....665
The createRadialGradient Method....666
Using Patterns for Fills....668
Writing Text to the Canvas....670
The strokeText Method....670
The textBaseline Property....671
The font Property....671
The textAlign Property....671
The fillText Method....672
The measureText Method....673
Drawing Lines....673
The lineWidth Property....674
The lineCap and lineJoin Properties....674
The miterLimit Property....676
Using Paths....676
The moveTo and lineTo Methods....677
The stroke Method....677
The rect Method....678
Filling Areas....678
The clip Method....680
The isPointInPath Method....683
Working with Curves....684
The arc Method....684
The arcTo Method....687
The quadraticCurveTo Method....688
The bezierCurveTo Method....689
Manipulating Images....690
The drawImage Method....690
Resizing an Image....691
Selecting an Image Area....691
Copying from a Canvas....693
Adding Shadows....693
Editing at the Pixel Level....695
The getImageData Method....695
The putImageData Method....698
The createImageData Method....698
Advanced Graphical Effects....699
The globalCompositeOperation Property....699
The globalAlpha Property....702
Transformations....702
The scale Method....703
The save and restore Methods....704
The rotate Method....704
The translate Method....705
The transform Method....706
The setTransform Method....708
Questions....709
Chapter 27. HTML5 Audio and Video....711
About Codecs....712
The Element....713
The Element....715
The Video Codecs....716
Questions....719
Chapter 28. Other HTML5 Features....721
Geolocation and the GPS Service....721
Other Location Methods....722
Geolocation and HTML5....723
Local Storage....726
Using Local Storage....726
The localStorage Object....727
Web Workers....729
Drag and Drop....732
Cross-Document Messaging....734
Other HTML5 Tags....738
Questions....738
Chapter 29. Bringing It All Together....739
Designing a Social Networking App....740
On the Website....740
functions.php....740
The Functions....741
header.php....743
setup.php....746
index.php....748
signup.php....749
Checking for Username Availability....749
Logging In....750
checkuser.php....753
login.php....754
profile.php....756
Adding the About Me Text....757
Adding a Profile Image....757
Processing the Image....757
Displaying the Current Profile....758
members.php....761
Viewing a Users Profile....762
Adding and Dropping Friends....762
Listing All Members....762
friends.php....765
messages.php....768
logout.php....771
styles.css....773
javascript.js....775
Questions....776
Appendix A. Solutions to the Chapter Questions....777
Chapter 1 Answers....777
Chapter 2 Answers....778
Chapter 3 Answers....778
Chapter 4 Answers....780
Chapter 5 Answers....780
Chapter 6 Answers....781
Chapter 7 Answers....782
Chapter 8 Answers....783
Chapter 9 Answers....784
Chapter 10 Answers....785
Chapter 11 Answers....785
Chapter 12 Answers....786
Chapter 13 Answers....787
Chapter 14 Answers....788
Chapter 15 Answers....788
Chapter 16 Answers....789
Chapter 17 Answers....790
Chapter 18 Answers....791
Chapter 19 Answers....791
Chapter 20 Answers....792
Chapter 21 Answers....793
Chapter 22 Answers....794
Chapter 23 Answers....795
Chapter 24 Answers....796
Chapter 25 Answers....797
Chapter 26 Answers....797
Chapter 27 Answers....799
Chapter 28 Answers....799
Chapter 29 Answers....800
Index....801
About the Author....824
Colophon....824
Build interactive, data-driven websites with the potent combination of open source technologies and web standards, even if you have only basic HTML knowledge. With the latest edition of this popular hands-on guide, you'll tackle dynamic web programming using the most recent versions of today's core technologies: PHP, MySQL, JavaScript, CSS, HTML5, jQuery, and the powerful React library.
Web designers will learn how to use these technologies together while picking up valuable web programming practices along the way, including how to optimize websites for mobile devices. You'll put everything together to build a fully functional social networking site suitable for both desktop and mobile browsers.