0% found this document useful (0 votes)
13 views

Contents

This document is about learning PHP, MySQL, JavaScript, CSS, and HTML5. It discusses setting up development environments on different platforms and provides introductions to programming concepts in PHP like variables, data types, operators, expressions and control flow.

Uploaded by

Juan Serrano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Contents

This document is about learning PHP, MySQL, JavaScript, CSS, and HTML5. It discusses setting up development environments on different platforms and provides introductions to programming concepts in PHP like variables, data types, operators, expressions and control flow.

Uploaded by

Juan Serrano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

FIFTH EDITION

Learning PHP, MySQL & JavaScript


With jQuery, CSS & HTML5

Robin Nixon

Beijing Boston Farnham Sebastopol Tokyo


Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

1. Introduction to Dynamic Web Content. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


HTTP and HTML: Berners-Lee’s Basics 2
The Request/Response Procedure 2
The Benefits of PHP, MySQL, JavaScript, CSS, and HTML5 5
MariaDB: The MySQL Clone 6
Using PHP 6
Using MySQL 7
Using JavaScript 8
Using CSS 10
And Then There’s HTML5 10
The Apache Web Server 11
Handling Mobile Devices 12
About Open Source 12
Bringing It All Together 13
Questions 14

2. Setting Up a Development Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17


What Is a WAMP, MAMP, or LAMP? 18
Installing AMPPS on Windows 18
Testing the Installation 22
Accessing the Document Root (Windows) 24
Alternative WAMPs 25
Installing AMPPS on macOS 26
Accessing the Document Root (macOS) 27
Installing a LAMP on Linux 28
Working Remotely 28

v
Logging In 29
Using FTP 29
Using a Program Editor 30
Using an IDE 31
Questions 33

3. Introduction to PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Incorporating PHP Within HTML 35
This Book’s Examples 37
The Structure of PHP 38
Using Comments 38
Basic Syntax 39
Variables 40
Operators 45
Variable Assignment 48
Multiple-Line Commands 50
Variable Typing 52
Constants 53
Predefined Constants 54
The Difference Between the echo and print Commands 55
Functions 55
Variable Scope 56
Questions 62

4. Expressions and Control Flow in PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63


Expressions 63
TRUE or FALSE? 64
Literals and Variables 65
Operators 66
Operator Precedence 67
Associativity 69
Relational Operators 70
Conditionals 74
The if Statement 75
The else Statement 76
The elseif Statement 78
The switch Statement 79
The ? Operator 82
Looping 83
while Loops 84
do...while Loops 86
for Loops 86

vi | Table of Contents
Breaking Out of a Loop 88
The continue Statement 89
Implicit and Explicit Casting 90
PHP Dynamic Linking 91
Dynamic Linking in Action 92
Questions 93

5. PHP Functions and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95


PHP Functions 96
Defining a Function 97
Returning a Value 98
Returning an Array 99
Passing Arguments by Reference 100
Returning Global Variables 102
Recap of Variable Scope 102
Including and Requiring Files 103
The include Statement 103
Using include_once 103
Using require and require_once 104
PHP Version Compatibility 104
PHP Objects 105
Terminology 106
Declaring a Class 107
Creating an Object 108
Accessing Objects 108
Cloning Objects 110
Constructors 111
Destructors 111
Writing Methods 112
Declaring Properties 113
Declaring Constants 113
Property and Method Scope 114
Static Methods 115
Static Properties 116
Inheritance 117
Questions 120

6. PHP Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123


Basic Access 123
Numerically Indexed Arrays 123
Associative Arrays 125
Assignment Using the array Keyword 126

Table of Contents | vii


The foreach...as Loop 127
Multidimensional Arrays 129
Using Array Functions 132
is_array 132
count 132
sort 133
shuffle 133
explode 133
extract 134
compact 135
reset 136
end 136
Questions 137

7. Practical PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139


Using printf 139
Precision Setting 140
String Padding 142
Using sprintf 143
Date and Time Functions 143
Date Constants 146
Using checkdate 146
File Handling 147
Checking Whether a File Exists 147
Creating a File 147
Reading from Files 149
Copying Files 150
Moving a File 150
Deleting a File 151
Updating Files 151
Locking Files for Multiple Accesses 153
Reading an Entire File 154
Uploading Files 155
System Calls 161
XHTML or HTML5? 162
Questions 163

8. Introduction to MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165


MySQL Basics 165
Summary of Database Terms 166
Accessing MySQL via the Command Line 166
Starting the Command-Line Interface 167

viii | Table of Contents


Using the Command-Line Interface 171
MySQL Commands 172
Data Types 177
Indexes 187
Creating an Index 187
Querying a MySQL Database 193
Joining Tables Together 203
Using Logical Operators 205
MySQL Functions 206
Accessing MySQL via phpMyAdmin 206
Questions 207

9. Mastering MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209


Database Design 209
Primary Keys: The Keys to Relational Databases 210
Normalization 211
First Normal Form 212
Second Normal Form 214
Third Normal Form 216
When Not to Use Normalization 218
Relationships 219
One-to-One 219
One-to-Many 220
Many-to-Many 221
Databases and Anonymity 222
Transactions 223
Transaction Storage Engines 223
Using BEGIN 224
Using COMMIT 225
Using ROLLBACK 225
Using EXPLAIN 226
Backing Up and Restoring 227
Using mysqldump 227
Creating a Backup File 229
Restoring from a Backup File 231
Dumping Data in CSV Format 231
Planning Your Backups 232
Questions 232

10. Accessing MySQL Using PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235


Querying a MySQL Database with PHP 235
The Process 235

Table of Contents | ix
Creating a Login File 236
Connecting to a MySQL Database 237
A Practical Example 243
The $_POST Array 246
Deleting a Record 247
Displaying the Form 247
Querying the Database 248
Running the Program 249
Practical MySQL 250
Creating a Table 251
Describing a Table 251
Dropping a Table 252
Adding Data 253
Retrieving Data 254
Updating Data 255
Deleting Data 255
Using AUTO_INCREMENT 256
Performing Additional Queries 257
Preventing Hacking Attempts 258
Steps You Can Take 259
Using Placeholders 260
Preventing HTML Injection 263
Using mysqli Procedurally 264
Questions 266

11. Form Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267


Building Forms 267
Retrieving Submitted Data 269
Default Values 270
Input Types 271
Sanitizing Input 278
An Example Program 280
HTML5 Enhancements 283
The autocomplete Attribute 283
The autofocus Attribute 283
The placeholder Attribute 283
The required Attribute 284
Override Attributes 284
The width and height Attributes 284
The min and max Attributes 284
The step Attribute 284
The form Attribute 285

x | Table of Contents
The list Attribute 285
The color Input Type 285
The number and range Input Types 285
Date and Time Pickers 286
Questions 286

12. Cookies, Sessions, and Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287


Using Cookies in PHP 287
Setting a Cookie 289
Accessing a Cookie 290
Destroying a Cookie 290
HTTP Authentication 290
Storing Usernames and Passwords 294
An Example Program 296
Using Sessions 299
Starting a Session 299
Ending a Session 302
Setting a Timeout 303
Session Security 304
Questions 307

13. Exploring JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309


JavaScript and HTML Text 310
Using Scripts Within a Document Head 311
Older and Nonstandard Browsers 311
Including JavaScript Files 312
Debugging JavaScript Errors 313
Using Comments 314
Semicolons 314
Variables 314
String Variables 315
Numeric Variables 315
Arrays 316
Operators 316
Arithmetic Operators 317
Assignment Operators 317
Comparison Operators 318
Logical Operators 318
Incrementing, Decrementing, and Shorthand Assignment 318
String Concatenation 318
Escape Characters 319
Variable Typing 319

Table of Contents | xi
Functions 321
Global Variables 321
Local Variables 321
The Document Object Model 322
Another Use for the $ Symbol 324
Using the DOM 325
About document.write 326
Using console.log 326
Using alert 326
Writing into Elements 326
Using document.write 327
Questions 327

14. Expressions and Control Flow in JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329


Expressions 329
Literals and Variables 330
Operators 331
Operator Precedence 332
Associativity 332
Relational Operators 333
The with Statement 336
Using onerror 337
Using try...catch 338
Conditionals 339
The if Statement 339
The else Statement 339
The switch Statement 340
The ? Operator 342
Looping 342
while Loops 342
do...while Loops 343
for Loops 344
Breaking Out of a Loop 345
The continue Statement 345
Explicit Casting 346
Questions 347

15. JavaScript Functions, Objects, and Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349


JavaScript Functions 349
Defining a Function 349
Returning a Value 351
Returning an Array 353

xii | Table of Contents


JavaScript Objects 354
Declaring a Class 354
Creating an Object 356
Accessing Objects 356
The prototype Keyword 356
JavaScript Arrays 359
Numeric Arrays 360
Associative Arrays 361
Multidimensional Arrays 362
Using Array Methods 363
Questions 368

16. JavaScript and PHP Validation and Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371


Validating User Input with JavaScript 371
The validate.html Document (Part 1) 372
The validate.html Document (Part 2) 374
Regular Expressions 377
Matching Through Metacharacters 378
Fuzzy Character Matching 378
Grouping Through Parentheses 379
Character Classes 380
Indicating a Range 380
Negation 380
Some More Complicated Examples 381
Summary of Metacharacters 383
General Modifiers 385
Using Regular Expressions in JavaScript 385
Using Regular Expressions in PHP 386
Redisplaying a Form After PHP Validation 387
Questions 393

17. Using Asynchronous Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395


What Is Asynchronous Communication? 396
Using XMLHttpRequest 396
Your First Asynchronous Program 399
Using GET Instead of POST 403
Sending XML Requests 406
Using Frameworks for Asynchronous Communication 411
Questions 411

18. Introduction to CSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413


Importing a Stylesheet 414

Table of Contents | xiii


Importing CSS from Within HTML 414
Embedded Style Settings 415
Using IDs 415
Using Classes 415
Using Semicolons 416
CSS Rules 416
Multiple Assignments 416
Using Comments 417
Style Types 418
Default Styles 418
User Styles 418
External Stylesheets 419
Internal Styles 419
Inline Styles 420
CSS Selectors 420
The Type Selector 420
The Descendant Selector 420
The Child Selector 421
The ID Selector 422
The Class Selector 423
The Attribute Selector 424
The Universal Selector 424
Selecting by Group 425
The CSS Cascade 425
Stylesheet Creators 426
Stylesheet Methods 426
Stylesheet Selectors 427
The Difference Between div and span Elements 429
Measurements 431
Fonts and Typography 433
font-family 433
font-style 434
font-size 434
font-weight 435
Managing Text Styles 435
Decoration 435
Spacing 436
Alignment 436
Transformation 436
Indenting 436
CSS Colors 437
Short Color Strings 438

xiv | Table of Contents


Gradients 438
Positioning Elements 440
Absolute Positioning 440
Relative Positioning 440
Fixed Positioning 441
Pseudoclasses 443
Shorthand Rules 445
The Box Model and Layout 446
Setting Margins 446
Applying Borders 448
Adjusting Padding 449
Object Contents 451
Questions 451

19. Advanced CSS with CSS3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453


Attribute Selectors 454
Matching Parts of Strings 454
The box-sizing Property 455
CSS3 Backgrounds 456
The background-clip Property 456
The background-origin Property 458
The background-size Property 458
Using the auto Value 459
Multiple Backgrounds 459
CSS3 Borders 461
The border-color Property 461
The border-radius Property 461
Box Shadows 465
Element Overflow 466
Multicolumn Layout 466
Colors and Opacity 468
HSL Colors 468
HSLA Colors 469
RGB Colors 469
RGBA Colors 469
The opacity Property 470
Text Effects 470
The text-shadow Property 470
The text-overflow Property 470
The word-wrap Property 471
Web Fonts 472
Google Web Fonts 473

Table of Contents | xv
Transformations 474
3D Transformations 475
Transitions 476
Properties to Transition 476
Transition Duration 477
Transition Delay 477
Transition Timing 477
Shorthand Syntax 478
Questions 480

20. Accessing CSS from JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481


Revisiting the getElementById Function 481
The O function 481
The S Function 482
The C Function 483
Including the Functions 484
Accessing CSS Properties from JavaScript 484
Some Common Properties 485
Other Properties 486
Inline JavaScript 488
The this Keyword 488
Attaching Events to Objects in a Script 489
Attaching to Other Events 490
Adding New Elements 491
Removing Elements 492
Alternatives to Adding and Removing Elements 493
Using Interrupts 494
Using setTimeout 494
Canceling a Timeout 495
Using setInterval 495
Using Interrupts for Animation 497
Questions 499

21. Introduction to jQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501


Why jQuery? 501
Including jQuery 502
Choosing the Right Version 502
Downloading 504
Using a Content Delivery Network 504
Customizing jQuery 505
jQuery Syntax 505
A Simple Example 506

xvi | Table of Contents


Avoiding Library Conflicts 507
Selectors 507
The css Method 508
The Element Selector 508
The ID Selector 509
The Class Selector 509
Combining Selectors 509
Handling Events 510
Waiting Until the Document Is Ready 511
Event Functions and Properties 512
The blur and focus Events 513
The this Keyword 514
The click and dblclick Events 514
The keypress Event 515
Considerate Programming 517
The mousemove Event 517
Other Mouse Events 520
Alternative Mouse Methods 521
The submit Event 522
Special Effects 523
Hiding and Showing 524
The toggle Method 525
Fading In and Out 526
Sliding Elements Up and Down 527
Animations 528
Stopping Animations 531
Manipulating the DOM 532
The Difference Between the text and html Methods 533
The val and attr Methods 533
Adding and Removing Elements 535
Dynamically Applying Classes 537
Modifying Dimensions 537
The width and height Methods 538
The innerWidth and innerHeight Methods 540
The outerWidth and OuterHeight Methods 540
DOM Traversal 541
Parent Elements 541
Child Elements 545
Sibling Elements 545
Selecting the Next and Previous Elements 547
Traversing jQuery Selections 549
The is Method 550

Table of Contents | xvii


Using jQuery Without Selectors 552
The $.each Method 552
The $.map Method 553
Using Asynchronous Communication 554
Using the POST Method 554
Using the GET Method 555
Plug-ins 555
jQuery User Interface 556
Other Plug-ins 556
Questions 556

22. Introduction to jQuery Mobile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559


Including jQuery Mobile 560
Getting Started 561
Linking Pages 563
Linking Synchronously 563
Linking Within a Multipage Document 564
Page Transitions 565
Styling Buttons 568
List Handling 571
Filterable Lists 572
List Dividers 574
What Next? 577
Questions 577

23. Introduction to HTML5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579


The Canvas 579
Geolocation 581
Audio and Video 583
Forms 584
Local Storage 585
Web Workers 585
Microdata 585
Questions 586

24. The HTML5 Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587


Creating and Accessing a Canvas 587
The toDataURL Function 589
Specifying an Image Type 591
The fillRect Method 591
The clearRect Method 591
The strokeRect Method 592

xviii | Table of Contents


Combining These Commands 592
The createLinearGradient Method 593
The addColorStop Method in Detail 595
The createRadialGradient Method 596
Using Patterns for Fills 598
Writing Text to the Canvas 600
The strokeText Method 600
The textBaseLine Property 601
The font Property 601
The textAlign Property 601
The fillText Method 602
The measureText Method 603
Drawing Lines 603
The lineWidth Property 604
The lineCap and lineJoin Properties 604
The miterLimit Property 606
Using Paths 606
The moveTo and lineTo Methods 607
The stroke Method 607
The rect Method 608
Filling Areas 608
The clip Method 610
The isPointInPath Method 613
Working with Curves 614
The arc Method 614
The arcTo Method 617
The quadraticCurveTo Method 618
The bezierCurveTo Method 619
Manipulating Images 620
The drawImage Method 620
Resizing an Image 621
Selecting an Image Area 621
Copying from a Canvas 623
Adding Shadows 623
Editing at the Pixel Level 625
The getImageData Method 625
The putImageData Method 628
The createImageData Method 628
Advanced Graphical Effects 629
The globalCompositeOperation Property 629
The globalAlpha Property 632
Transformations 632

Table of Contents | xix


The scale Method 633
The save and restore Methods 634
The rotate Method 634
The translate Method 635
The transform Method 636
The setTransform Method 638
Questions 639

25. HTML5 Audio and Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641


About Codecs 642
The <audio> Element 643
Supporting Non-HTML5 Browsers 646
The <video> Element 647
The Video Codecs 648
Supporting Older Browsers 651
Questions 653

26. Other HTML5 Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655


Geolocation and the GPS Service 655
Other Location Methods 656
Geolocation and HTML5 657
Local Storage 660
Using Local Storage 661
The localStorage Object 661
Web Workers 663
Drag and Drop 666
Cross-Document Messaging 668
Other HTML5 Tags 672
Questions 672

27. Bringing It All Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675


Designing a Social Networking App 676
On the Website 676
functions.php 676
The Functions 677
header.php 679
setup.php 682
index.php 683
signup.php 684
Checking for Username Availability 685
Logging In 685
checkuser.php 688

xx | Table of Contents
login.php 689
profile.php 691
Adding the “About Me” Text 692
Adding a Profile Image 692
Processing the Image 692
Displaying the Current Profile 693
members.php 696
Viewing a User’s Profile 696
Adding and Dropping Friends 697
Listing All Members 697
friends.php 700
messages.php 703
logout.php 706
styles.css 708
javascript.js 710

A. Solutions to the Chapter Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

B. Online Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735

C. MySQL’s FULLTEXT Stopwords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739

D. MySQL Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743

E. jQuery Selectors, Objects, and Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

Table of Contents | xxi

You might also like