Table Of Content4
t
h
wi E
t d
h j iti
Q o
u
e n
r
y
■ Learning
■
■ P HP, MySQL
■
■
& JavaScript
■
■
■
■
WITH JQUERY, CSS & HTML5
Robin Nixon
Learning PHP, MySQL & JavaScript
“
Build interactive, data-driven websites with the potent combination of This is a great beginner's
open-source technologies and web standards, even if you have only basic book that introduces
HTML knowledge. With this popular hands-on guide, you’ll tackle dynamic
several crucial web
web programming with the help of today’s core technologies: PHP, MySQL,
developer languages.
JavaScript, jQuery, CSS, and HTML5.
It's a quick-paced, easy-
Explore each technology separately, learn how to use them together, and
pick up valuable web programming practices along the way. At the end of to-read, information-
the book, you’ll put everything together to build a fully functional social packed book that will
networking site, using XAMPP or any development stack you choose.
soon have you creating
dynamically driven web-
■ Learn PHP in-depth, along with the basics of object-oriented
programming sites, including a basic
”
■ Explore MySQL, from database structure to complex queries social networking site.
■ Use the MySQLi Extension, PHP’s improved MySQL interface —Albert Wiersch
developer of CSE HTML Validator
■ Create dynamic PHP web pages that tailor themselves to
the user
■ Manage cookies and sessions, and maintain a high level
of security
■ Master the JavaScript language—and enhance it with jQuery
■ Use Ajax calls for background browser/server communication
■ Acquire CSS2 & CSS3 skills for professionally styling your
web pages
■ Implement all of the new HTML5 features, including
geolocation, audio, video, and the canvas
Robin Nixon, an IT journalist who has written hundreds of articles and several
books on computing, has developed numerous websites using open source tools,
specializing in the technologies featured in this book. Robin has worked with and
written about computers since the early 1980s.
WEB DEVELOPMENT Twitter: @oreillymedia
facebook.com/oreilly
US $49.99 CAN $52.99
ISBN: 978-1-491-91866-1
FOURTH EDITION
Learning PHP, MySQL & JavaScript
With jQuery, CSS & HTML5
Robin Nixon
Learning PHP, MySQL & JavaScript
With jQuery, CSS & HTML5
by Robin Nixon
Copyright © 2015 Robin Nixon. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editor: Andy Oram Indexer: Ellen Troutman
Production Editor: Nicole Shelby Interior Designer: David Futato
Copyeditor: Rachel Monaghan Cover Designer: Randy Comer
Proofreader: Sharon Wilkey Illustrator: Rebecca Demarest
December 2014: Fourth Edition
Revision History for the Fourth Edition
2014-11-21: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491918661 for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning PHP, MySQL & JavaScript, the
cover image, and related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the author disclaim all responsibility
for errors or omissions, including without limitation responsibility for damages resulting from the use of
or reliance on this work. Use of the information and instructions contained in this work is at your own
risk. If any code samples or other technology this work contains or describes is subject to open source
licenses or the intellectual property rights of others, it is your responsibility to ensure that your use
thereof complies with such licenses and/or rights.
978-1-491-91866-1
[LSI]
For Julie
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
Using PHP 6
Using MySQL 7
Using JavaScript 8
Using CSS 9
And Then There’s HTML5 10
The Apache Web Server 11
About Open Source 12
Bringing It All Together 12
Questions 14
2. Setting Up a Development Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
What Is a WAMP, MAMP, or LAMP? 16
Installing XAMPP on Windows 16
Testing the Installation 24
Installing XAMPP on Mac OS X 27
Accessing the Document Root 27
Installing a LAMP on Linux 28
Working Remotely 28
Logging In 28
Using FTP 29
Using a Program Editor 30
Using an IDE 31
v
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? 63
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
Breaking Out of a Loop 88
The continue Statement 89
Implicit and Explicit Casting 90
PHP Dynamic Linking 91
vi | Table of Contents
Dynamic Linking in Action 92
Questions 93
5. PHP Functions and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
PHP Functions 96
Defining a Function 98
Returning a Value 98
Returning an Array 100
Do Not Pass Arguments by Reference 100
Returning Global Variables 102
Recap of Variable Scope 103
Including and Requiring Files 103
The include Statement 104
Using include_once 104
Using require and require_once 105
PHP Version Compatibility 105
PHP Objects 106
Terminology 106
Declaring a Class 107
Creating an Object 108
Accessing Objects 109
Cloning Objects 110
Constructors 111
PHP 5 Destructors 112
Writing Methods 112
Static Methods in PHP 5 113
Declaring Properties 114
Declaring Constants 115
Property and Method Scope in PHP 5 115
Static Properties and Methods 116
Inheritance 118
Questions 121
6. PHP Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Basic Access 123
Numerically Indexed Arrays 123
Associative Arrays 125
Assignment Using the array Keyword 126
The foreach...as Loop 127
Multidimensional Arrays 129
Using Array Functions 132
is_array 132
Table of Contents | vii
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 152
Reading an Entire File 154
Uploading Files 155
System Calls 160
XHTML or HTML5? 162
Questions 162
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
Using the Command-Line Interface 171
MySQL Commands 172
Data Types 177
Indexes 186
viii | Table of Contents