Tải bản đầy đủ (.pdf) (724 trang)

Php Mysql Javascript Html5 5 in 1

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (13.86 MB, 724 trang )



PHP,
MySQL ,
Javascript
& HTML5
®

®

ALL-IN- ONE



PHP,
MySQL ,
Javascript
& HTML5
®

®

ALL-IN- ONE

by Steve Suehring and Janet Valade


PHP, MySQL®, JavaScript® & HTML5 All-in-One For Dummies®
Published by
John Wiley & Sons, Inc.
111 River Street


Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Hoboken, New Jersey
Published by John Wiley & Sons, Inc., Hoboken, New Jersey
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley
& Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://
www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!,
The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and
related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates
in the United States and other countries, and may not be used without written permission. MySQL is a
registered trademark of MySQL AB. JavaScript is a registered trademark of Oracle America, Inc. All other
trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any
product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF
THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE
CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES
CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF
A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE
AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION
OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE.
FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE

CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material
included with standard print versions of this book may not be included in e-books or in print-on-demand.
If this book refers to media such as a CD or DVD that is not included in the version you purchased, you
may download this material at . For more information about Wiley
products, visit www.wiley.com.
Library of Congress Control Number: 2013932114
ISBN 978-1-118-21370-4 (pbk); ISBN 978-1-118-22874-6 (ebk); ISBN 978-1-118-23134-0 (ebk);
ISBN 978-1-118-26617-5 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1


About the Authors
Steve Suehring is the author of several technology books. Steve has written
web applications, big and small, for a variety of organizations and in a variety
of programming languages. Steve’s expertise is in finding creative solutions
to complex problems and complex solutions to simple problems.
Janet Valade is the author of PHP & MySQL For Dummies, which is in its third
edition. She has also written PHP & MySQL Everyday Apps For Dummies and
PHP & MySQL: Your visual blueprint for creating dynamic, database-driven Web
sites. In addition, Janet is the author of Spring into Linux and a coauthor of
Mastering Visually Dreamweaver CS3 and Flash CS3 Professional.
Janet has 20 years of experience in the computing field. Most recently, she
worked as a Web designer and programmer in an engineering firm for four
years. Prior to that, Janet worked for 13 years in a university environment,
where she was a systems analyst. During her tenure, she supervised the

installation and operation of computing resources, designed and developed
a data archive, supported faculty and students in their computer usage,
wrote numerous technical papers, and developed and presented seminars
on a variety of technology topics.



Dedication
To Bob and Mary.
– Steve Suehring
This book is dedicated to everyone who finds it useful.
– Janet Valade

Authors’ Acknowledgments
From Steve Suehring: For these acknowledgements, I decided to look back
at the acknowledgements section that I wrote more than 10 years ago for my
first book, MySQL Bible. I was curious who, of all of the people I thanked in
that book (and there were a lot), should be thanked in this book, 10+ years
later. The answer: All of them. They (and you, the reader) have contributed
to my ability to continue to write books (and articles, and blog posts, and
everything else.) I look forward to continued success together.
From Janet Valade: First, I wish to express my appreciation to the entire
open source community. Without those who give their time and talent, there
would be no cool PHP and MySQL for me to write about. Furthermore, I never
would have learned this software without the lists where people generously
spend their time answering foolish questions from beginners.
I want to thank my mother for passing on a writing gene, along with many
other things. And my children always for everything.
And, of course, I want to thank the professionals who make it all possible.
Without my agent and the people at Wiley Publishing, Inc., this book would

not exist. Because they all do their jobs so well, I can contribute my part to
this joint project.


Publisher’s Acknowledgments
We’re proud of this book; please send us your comments at .
For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974,
outside the U.S. at 317-572-3993, or fax 317-572-4002.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and
Vertical Websites

Composition Services

Project Editor: Heidi Unger
(Previous Edition: Jean Nelson)
Acquisitions Editor: Kyle Looper
Copy Editor: Debbye Butler

Project Coordinator: Patrick Redmond
Layout and Graphics: Jennifer Creasey
Proofreaders: Jessica Kramer, Sossity R. Smith
Indexer: BIM Indexing & Proofreading Services

Technical Editor: Peter Veverka
Editorial Manager: Kevin Kirschner
Vertical Websites: Richard Graves
Editorial Assistant: Annie Sullivan
Sr. Editorial Assistant: Cherie Case
Cover Photo: © pagadesign/iStockphoto


Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Kathleen Nebenhaus, Vice President and Executive Publisher
Composition Services
Debbie Stailey, Director of Composition Services


Contents at a Glance
Introduction................................................................. 1
Book I: Getting Started with PHP & MySQL.................... 5
Chapter 1: Understanding the Languages of the Web.................................................... 7
Chapter 2: Installing a Web Server................................................................................. 21
Chapter 3: Installing PHP................................................................................................. 35
Chapter 4: Setting Up MySQL.......................................................................................... 55
Chapter 5: Setting Up Your Web Development Environment
with the XAMPP Package............................................................................................... 75

Book II: HTML and CSS............................................... 87
Chapter 1: Creating a Basic Page with HTML................................................................ 89
Chapter 2: Adding Style with CSS................................................................................. 121
Chapter 3: Creating and Styling Web Forms............................................................... 169

Book III: JavaScript.................................................. 185
Chapter 1: Understanding JavaScript Basics.............................................................. 187
Chapter 2: Building a JavaScript Program................................................................... 191

Chapter 3: Adding jQuery.............................................................................................. 219
Chapter 4: Reacting to Events with JavaScript and jQuery....................................... 241
Chapter 5: Troubleshooting JavaScript Programs..................................................... 261

Book IV: PHP........................................................... 269
Chapter 1: Understanding PHP Basics......................................................................... 271
Chapter 2: Building PHP Scripts................................................................................... 319
Chapter 3: PHP and Your Operating System............................................................... 365
Chapter 4: Object-Oriented Programming................................................................... 397
Chapter 5: Considering PHP Security........................................................................... 425
Chapter 6: Tracking Visitors with Sessions................................................................. 437

Book V: MySQL........................................................ 447
Chapter 1: Introducing MySQL...................................................................................... 449
Chapter 2: Administering MySQL................................................................................. 457
Chapter 3: Designing and Building a Database........................................................... 475
Chapter 4: Using the Database...................................................................................... 497
Chapter 5: Communicating with the Database from PHP Scripts............................. 515


Book VI: Web Applications........................................ 529
Chapter 1: Improving Your PHP Programs.................................................................. 531
Chapter 2: Creating and Using a Web Service............................................................. 541
Chapter 3: Validating Web Forms with JavaScript and PHP..................................... 555
Chapter 4: Building a Members-Only Website............................................................ 587

Book VII: PHP and Templates.................................... 633
Chapter 1: Configuring PHP........................................................................................... 635
Chapter 2: Building a Templating System.................................................................... 641


Index....................................................................... 655


Table of Contents
Introduction.................................................................. 1
About This Book............................................................................................... 1
Foolish Assumptions........................................................................................ 1
How This Book Is Organized........................................................................... 2
Book I: Getting Started with PHP and MySQL..................................... 2
Book II: HTML and CSS........................................................................... 2
Book III: JavaScript................................................................................. 2
Book IV:PHP............................................................................................. 2
Book V: MySQL........................................................................................ 2
Book VI: Web Applications.................................................................... 2
Book VII: PHP and Templates................................................................ 2
Companion Website............................................................................... 3
Icons Used in This Book.................................................................................. 3
Where to Go from Here.................................................................................... 3

Book I: Getting Started with PHP & MySQL.................... 5
Chapter 1: Understanding the Languages of the Web . . . . . . . . . . . . . . 7
Understanding How the Web Works.............................................................. 7
The web browser.................................................................................... 8
The web server....................................................................................... 8
Understanding Web Page Languages........................................................... 10
Marking up with HTML......................................................................... 10
Styling pages with CSS......................................................................... 11
Changing behaviors with JavaScript.................................................. 11
Understanding the Language of Web Servers............................................. 12
Building dynamic web applications with PHP and MySQL............. 12

Sending the page to the browser with Apache................................. 13
Choosing How You Want to Develop........................................................... 14
Choosing a host for your website....................................................... 14
Hosting for a company website.......................................................... 15
Choosing a web-hosting company...................................................... 16
Using a hosted website........................................................................ 18
Setting Up Your Local Computer for Development.................................... 19
Installing the web server...................................................................... 19
Installing PHP........................................................................................ 20
Installing MySQL................................................................................... 20


xii

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies
Chapter 2: Installing a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Testing Your Web Server............................................................................... 21
Obtaining Apache........................................................................................... 22
Selecting a version of Apache............................................................. 22
Downloading from the Apache website............................................. 23
Obtaining Apache for Windows.......................................................... 23
Obtaining Apache for Linux................................................................. 23
Obtaining Apache for Mac................................................................... 24
Obtaining all-in-one installation kits................................................... 24
Verifying a downloaded file................................................................. 24
Installing Apache............................................................................................ 25
Installing Apache on Windows............................................................ 25
Installing Apache on a Mac.................................................................. 27
Installing Apache from source code on Linux and Mac................... 27
Starting and Stopping Apache...................................................................... 28

Starting and stopping Apache on Windows...................................... 28
Starting Apache on Linux, Unix, and Mac.......................................... 29
Restarting Apache on Linux, Unix, and Mac...................................... 30
Stopping Apache on Linux, Unix, and Mac........................................ 30
Getting Information from Apache................................................................. 31
Getting Apache information on Windows.......................................... 31
Getting Apache information on Linux, Unix, and Mac..................... 31
Configuring Apache........................................................................................ 32
Changing settings.................................................................................. 32
Changing the location of your Document Root................................. 33
Changing the port number.................................................................. 33

Chapter 3: Installing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Checking the PHP Installation....................................................................... 36
Obtaining PHP................................................................................................. 36
Downloading from the PHP website................................................... 37
Obtaining PHP for Windows................................................................ 37
Obtaining PHP for Linux...................................................................... 37
Obtaining PHP for the Mac OS............................................................ 38
Obtaining all-in-one installation kits................................................... 38
Verifying a downloaded file................................................................. 39
Installing PHP.................................................................................................. 40
Installing on Unix and Linux................................................................ 40
Installing on Mac OS X.......................................................................... 42
Installation options for Unix, Linux, and Mac................................... 44
Installing on Windows.......................................................................... 46
Configuring Your Web Server for PHP......................................................... 47
Configuring your web server on Windows........................................ 47
Configuring Apache on Linux and Mac.............................................. 49
Configuring PHP.............................................................................................. 50



Table of Contents

xiii

Testing PHP..................................................................................................... 51
Troubleshooting............................................................................................. 53
Unable to change PHP settings........................................................... 53
Displays error message: Undefined function.................................... 53
Displays a blank page or HTML output only..................................... 53

Chapter 4: Setting Up MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Checking the MySQL Installation.................................................................. 55
Finding out if MySQL is running or installed..................................... 56
Starting MySQL...................................................................................... 56
Obtaining MySQL............................................................................................ 57
Downloading from the MySQL website.............................................. 58
Obtaining MySQL for Windows........................................................... 58
Obtaining MySQL for Linux and Unix................................................. 58
Obtaining MySQL for Mac.................................................................... 59
Obtaining all-in-one installation kits................................................... 59
Verifying a downloaded file................................................................. 59
Installing MySQL............................................................................................. 59
Running the MySQL Setup Wizard on Windows............................... 60
Installing MySQL on Linux from an RPM file..................................... 61
Installing MySQL on Mac from a DMG file......................................... 62
Installing MySQL from source files..................................................... 63
Configuring MySQL......................................................................................... 65
Starting and Stopping the MySQL Server.................................................... 66

Controlling the server on Windows.................................................... 66
Controlling the MySQL server on Linux and Mac............................. 67
Testing MySQL................................................................................................ 68
Troubleshooting MySQL................................................................................ 69
Displays error message: Access denied............................................. 69
Displays error message: Client does not support
authentication protocol................................................................... 69
Displays error message: Can’t connect to . . .................................... 70
MySQL error log.................................................................................... 70
The MySQL Administration Program........................................................... 70
Activating MySQL Support............................................................................ 71
Activating MySQL support on Windows............................................ 71
Activating MySQL support on Linux and the Mac OS...................... 71
Checking MySQL support.................................................................... 72
Troubleshooting PHP and MySQL................................................................ 73
Displays error message: Undefined function.................................... 73
MySQL functions not activated (Windows)....................................... 74

Chapter 5: Setting Up Your Web Development Environment
with the XAMPP Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Obtaining XAMPP........................................................................................... 75
Installing XAMPP............................................................................................ 76


xiv

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies
Using the XAMPP Control Panel................................................................... 78
Testing Your Development Environment.................................................... 79
Opening the XAMPP web page............................................................ 80

Testing phpMyAdmin........................................................................... 81
Testing PHP........................................................................................... 81
Configuring Your Development Environment............................................. 82
Configuring PHP.................................................................................... 83
Configuring Apache.............................................................................. 83
Configuring MySQL............................................................................... 84
Uninstalling and Reinstalling XAMPP........................................................... 84
Troubleshooting............................................................................................. 85

Book II: HTML and CSS................................................ 87
Chapter 1: Creating a Basic Page with HTML . . . . . . . . . . . . . . . . . . . . 89
Understanding the HTML Building Blocks.................................................. 89
Document types.................................................................................... 90
Sections of an HTML Document................................................................... 91
The root element.................................................................................. 92
The head section and title element.................................................... 92
The body section.................................................................................. 94
Creating Good HTML...................................................................................... 94
Using the appropriate elements......................................................... 94
Putting text on a page.......................................................................... 95
Creating your first page....................................................................... 97
Choosing block-level or inline elements............................................ 98
Inserting line breaks and spaces........................................................ 99
Making your document easier to maintain...................................... 101
Adding lists and tables....................................................................... 102
Practicing Creating a Table......................................................................... 105
Including Links and Images on Your Web Page........................................ 108
Adding links......................................................................................... 108
Adding images..................................................................................... 113
Writing Valid HTML...................................................................................... 116

Validating Your HTML................................................................................. 117

Chapter 2: Adding Style with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Discovering What CSS Can and Can’t Do for Your Web Page................. 121
What is CSS?........................................................................................ 121
Why use CSS?...................................................................................... 122
Limitations of CSS............................................................................... 122
Connecting CSS to a Page............................................................................ 123
Adding styling to an HTML element................................................. 123
Using an internal style sheet............................................................. 126
Using an external style sheet............................................................ 128


Table of Contents

xv

Targeting Styles............................................................................................ 129
Selecting HTML elements.................................................................. 130
Selecting individual elements............................................................ 130
Selecting a group of elements........................................................... 131
Changing Fonts............................................................................................. 134
Setting the font family........................................................................ 134
Setting font size................................................................................... 136
Setting the font color.......................................................................... 138
Adding Borders............................................................................................. 140
Changing List Styles..................................................................................... 144
Changing bullet styles........................................................................ 145
Removing bullets................................................................................ 146
Adding a Background................................................................................... 147

Changing the background color........................................................ 147
Adding a background image.............................................................. 150
Creating Page Layouts................................................................................. 155
Creating a single-column layout........................................................ 155
Creating a two-column layout........................................................... 159
Adding Headers and Footers to a Page...................................................... 163
Creating a header, header menu, and footer.................................. 163
Examining the HTML and CSS files................................................... 166

Chapter 3: Creating and Styling Web Forms . . . . . . . . . . . . . . . . . . . . . 169
Using Web Forms to Get Information......................................................... 169
Understanding web forms................................................................. 170
Looking at form elements.................................................................. 170
Creating a Form............................................................................................ 172
All about the form element................................................................ 172
Adding a text input............................................................................. 173
Adding a drop-down box................................................................... 174
Creating check boxes......................................................................... 176
Using radio buttons............................................................................ 178
Submitting and clearing the form..................................................... 179
Using CSS to Align Form Fields................................................................... 180

Book III: JavaScript................................................... 185
Chapter 1: Understanding JavaScript Basics . . . . . . . . . . . . . . . . . . . 187
Viewing the World of JavaScript................................................................. 187
JavaScript isn’t Java........................................................................... 187
Knowing what JavaScript can do...................................................... 188
Examining the Ways to Add JavaScript to a Page.................................... 188
Adding the JavaScript tag.................................................................. 189
Adding JavaScript to a page’s HTML................................................ 189

Using external JavaScript.................................................................. 190


xvi

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies
Chapter 2: Building a JavaScript Program . . . . . . . . . . . . . . . . . . . . . . 191
Getting Started with JavaScript Programming.......................................... 191
Sending an alert to the screen.......................................................... 191
Adding comments............................................................................... 193
Holding data for later in variables.................................................... 193
Holding multiple values in an array................................................. 195
Creating strings to keep track of words........................................... 195
Working with numbers....................................................................... 196
Testing Things with Conditionals............................................................... 197
Performing Actions Multiple Times with Loops....................................... 200
For what it’s worth............................................................................. 200
While you’re here................................................................................ 203
Using Functions to Avoid Repeating Yourself.......................................... 203
Creating functions.............................................................................. 204
Adding function arguments............................................................... 204
Calling a function................................................................................ 204
Improving the addNumbers function............................................... 205
Returning results from functions...................................................... 207
Objects in Brief............................................................................................. 208
Creating objects.................................................................................. 208
Adding properties to objects............................................................ 209
Working with HTML Documents................................................................. 210
Accessing HTML with JavaScript...................................................... 211
Using GetElementById to access a specific element...................... 211

Working with Web Browsers....................................................................... 214
Detecting the browser........................................................................ 214
Redirecting to another page.............................................................. 216

Chapter 3: Adding jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
jQuery Introduced........................................................................................ 219
Installing jQuery............................................................................................ 220
Installing jQuery locally..................................................................... 220
Using CDN-hosted jQuery.................................................................. 221
Adding jQuery to a Page.............................................................................. 221
Adding local jQuery to a page........................................................... 221
Adding CDN jQuery to a page............................................................ 222
Incorporating the jQuery ready() Function.............................................. 223
Selecting Elements with jQuery.................................................................. 225
jQuery selectors up close.................................................................. 226
Filtering................................................................................................ 227
Working with HTML Using jQuery.............................................................. 227
Adding HTML to a page...................................................................... 227
Changing elements............................................................................. 230
Changing Attributes and Styles.................................................................. 232
Reading attributes.............................................................................. 233
Writing attributes............................................................................... 234
Changing CSS....................................................................................... 237


Table of Contents

xvii

Chapter 4: Reacting to Events with JavaScript and jQuery . . . . . . . . 241

Understanding Events.................................................................................. 241
Working with Forms..................................................................................... 242
Adding a Submit Handler................................................................... 242
Checking for blank fields.................................................................... 246
Monitoring Mouse Events............................................................................ 247
Capturing mouse clicks...................................................................... 247
Watching mouse movements............................................................ 251
Reacting to Keyboard Events...................................................................... 254
Counting characters........................................................................... 254
Preventing character input................................................................ 257

Chapter 5: Troubleshooting JavaScript Programs . . . . . . . . . . . . . . . 261
Employing Basic JavaScript Troubleshooting Techniques..................... 261
Adding alerts....................................................................................... 262
Using comments in JavaScript.......................................................... 262
Identifying JavaScript Problems with Firebug.......................................... 264
Installing Firebug................................................................................ 264
Using Firebug....................................................................................... 266

Book IV: PHP............................................................ 269
Chapter 1: Understanding PHP Basics . . . . . . . . . . . . . . . . . . . . . . . . . 271
How PHP Works............................................................................................ 271
Examining the Structure of a PHP Script................................................... 273
Looking at PHP Syntax................................................................................. 275
Using simple statements.................................................................... 276
Using complex statements................................................................. 276
Writing PHP Code......................................................................................... 277
Displaying Content in a Web Page.............................................................. 278
Using PHP Variables..................................................................................... 281
Naming a variable............................................................................... 282

Creating and assigning values to variables..................................... 282
Using variable variables..................................................................... 283
Displaying variable values................................................................. 284
Using PHP Constants.................................................................................... 287
Understanding Data Types.......................................................................... 288
Working with integers and floating-point numbers........................ 289
Working with character strings........................................................ 292
Working with the Boolean data type................................................ 295
Working with the NULL data type.................................................... 296
Using Arrays.................................................................................................. 296
Creating arrays.................................................................................... 296
Viewing arrays..................................................................................... 298
Removing values from arrays............................................................ 299


xviii

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies
Sorting arrays...................................................................................... 299
Getting values from arrays................................................................ 301
Walking through an array.................................................................. 302
Storing values with multidimensional arrays.................................. 305
Using Dates and Times................................................................................. 307
Setting local time................................................................................ 307
Formatting a date................................................................................ 308
Storing a timestamp in a variable..................................................... 309
Understanding PHP Error Messages.......................................................... 310
Types of PHP error messages........................................................... 310
Displaying error messages................................................................ 313
Logging error messages..................................................................... 315

Adding Comments to Your PHP Script...................................................... 316

Chapter 2: Building PHP Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Setting Up Conditions.................................................................................. 320
Comparing values............................................................................... 320
Checking variable content................................................................. 322
Pattern matching with regular expressions.................................... 323
Joining multiple comparisons........................................................... 327
Using Conditional Statements..................................................................... 329
Using if statements............................................................................. 330
Using switch statements.................................................................... 333
Repeating Actions with Loops.................................................................... 335
Using for loops.................................................................................... 335
Using while loops................................................................................ 339
Using do..while loops......................................................................... 341
Avoiding infinite loops....................................................................... 343
Breaking out of a loop........................................................................ 344
Using Functions............................................................................................ 346
Creating a function............................................................................. 347
Using variables in functions.............................................................. 347
Passing values to a function.............................................................. 349
Returning a value from a function.................................................... 354
Using built-in functions...................................................................... 356
Organizing Scripts........................................................................................ 357
Separating display code from logic code......................................... 357
Reusing code....................................................................................... 358
Organizing with functions.................................................................. 358
Organizing with include files............................................................. 359

Chapter 3: PHP and Your Operating System . . . . . . . . . . . . . . . . . . . . . 365

Managing Files............................................................................................... 366
Getting information about files......................................................... 366
Copying, renaming, and deleting files.............................................. 368
Organizing files.................................................................................... 369



×