Table Of Content00 fm 11/29/01 3:19 PM Page i
Matt Zandstra
Teach Yourself
PHP
24
in Hours
SECOND EDITION
800 East 96th St.,Indianapolis,Indiana,4624 0 USA
00 fm 11/29/01 3:19 PM Page ii
Sams Teach Yourself PHP in 24 Hours, ACQUISITIONSEDITOR
Scott D. Meyers
Second Edition
DEVELOPMENTEDITORS
Copyright © 2002 by Sams Publishing Scott D. Meyers
Jill Hayden
All rights reserved. No part of this book shall be reproduced,stored in a
retrieval system,or transmitted by any means,electronic,mechanical,photo- MANAGINGEDITOR
copying,recording,or otherwise,without written permission from the pub- Charlotte Clapp
lisher. No patent liability is assumed with respect to the use of the information
INDEXER
contained herein. Although every precaution has been taken in the preparation
D&G Limited,LLC
of this book,the publisher and author assume no responsibility for errors or
omissions. Nor is any liability assumed for damages resulting from the use of PROOFREADER
the information contained herein. D&G Limited,LLC
International Standard Book Number:0-672-32311-7 TECHNICALEDITOR
Andrei Zmievski
Library of Congress Catalog Card Number:2001096665
TEAMCOORDINATOR
Printed in the United States of America
Amy Patton
First Printing:December 2001
INTERIORDESIGNER
04 03 02 01 4 3 2 1 Gary Adair
Trademarks COVERDESIGNER
Aren Howell
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to the PRODUCTION
accuracy of this information. Use of a term in this book should not be regarded D&G Limited,LLC
as affecting the validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as
possible,but no warranty or fitness is implied. The information provided is on
an “as is”basis. The author and the publisher shall have neither liability nor
responsibility to any person or entity with respect to any loss or damages aris-
ing from the information contained in this book or from the use of the pro-
grams accompanying it.
00 fm 11/29/01 3:19 PM Page iii
Contents at a Glance
Introduction 1
Part I Getting Started 5
Hour 1 PHP:From Homepage to Portal 7
2 Installing PHP 15
3 A First Script 27
Part II The Language 37
Hour 4 The Building Blocks 39
5 Going with the Flow 59
6 Functions 77
7 Arrays 99
8 Objects 119
Part III Working with PHP 147
Hour 9 Working with Forms 149
10 Working with Files 169
11 Working with DBA Functions 189
12 Database Integration—SQL 207
13 Beyond the Box 235
14 Images On-the-Fly 255
15 Working with Dates and Times 277
16 Working with Data 297
17 Working with Strings 319
18 Working with Regular Expressions 341
19 Saving State with Cookies and Query Strings 361
20 Saving State with Session Functions 379
21 Working with the Server Environment 393
22 PHP 4 and XML 407
00 fm 11/29/01 3:19 PM Page iv
Part IV Extending PHP 429
Hour 23 Smarty:A Template Engine 431
24 An Example:Page.inc.php 451
Glossary 473
Index 481
00 fm 11/29/01 3:19 PM Page v
Contents
Introduction 1
PART I Getting Started 5
HOUR 1 PHP: From Homepage to Portal 7
What Is PHP? ..........................................................................................................8
What Need Does PHP Fulfill? ................................................................................8
What’s New in PHP 4 ............................................................................................9
The Zend Engine ..................................................................................................10
Why Choose PHP? ..............................................................................................10
Speed of Development ....................................................................................10
PHP Is Open Source ........................................................................................11
Performance ....................................................................................................11
Portability ........................................................................................................11
What’s New in This Edition ................................................................................11
Summary ..............................................................................................................12
Q&A ......................................................................................................................12
Workshop ..............................................................................................................12
Quiz ................................................................................................................12
Quiz Answers ..................................................................................................13
Activities ..........................................................................................................13
HOUR 2 Installing PHP 15
Platforms,Servers,Databases,and PHP ..............................................................15
Where to Find PHP and More ..............................................................................16
Installing PHP 4 for Linux and Apache ..............................................................17
SomeconfigureOptions ..................................................................................18
--with-gdbm..................................................................................................18
--with-gd ......................................................................................................19
--with-ttf....................................................................................................19
--with-mysql ..............................................................................................19
XML configure options ........................................................................................20
Configuring Apache ..............................................................................................20
php.ini ..............................................................................................................21
short_open_tag ..........................................................................................22
Error Reporting Directives ..............................................................................22
Variable Directives ..........................................................................................22
Changingphp.iniDirectives Locally ................................................................23
Help! ....................................................................................................................23
00 fm 11/29/01 3:19 PM Page vi
vi Sams Teach Yourself PHP in 24 Hours
Summary ..............................................................................................................24
Q&A ......................................................................................................................25
Workshop ..............................................................................................................25
Quiz ................................................................................................................25
Quiz Answers ..................................................................................................25
Activities ..........................................................................................................26
HOUR 3 A First Script 27
Our First Script ....................................................................................................27
Beginning and Ending a Block of PHP Statements ........................................29
Theprint()Function ..................................................................................31
Combining HTML and PHP ................................................................................32
Adding Comments to PHP Code ..........................................................................33
Summary ..............................................................................................................34
Q&A ......................................................................................................................34
Workshop ..............................................................................................................35
Quiz ................................................................................................................35
Quiz Answers ..................................................................................................35
Activities ..........................................................................................................35
PART II The Language 37
HOUR 4 The Building Blocks 39
Variables ................................................................................................................40
Data Types ............................................................................................................41
Changing Type with settype() ....................................................................43
Changing Type by Casting ..............................................................................44
Why Test Type? ..............................................................................................46
Operators and Expressions ..................................................................................46
The Assignment Operator ................................................................................47
Arithmetic Operators ......................................................................................47
The Concatenation Operator ..........................................................................48
Combined Assignment Operators ....................................................................48
Comparison Operators ....................................................................................49
Creating More Complex Test Expressions with the Logical
Operators ......................................................................................................50
Automatically Incrementing and Decrementing an Integer
Variable ........................................................................................................51
Operator Precedence ........................................................................................53
Constants ..............................................................................................................54
Predefined Constants ......................................................................................55
Summary ..............................................................................................................55
Q&A ......................................................................................................................56
Workshop ..............................................................................................................56
00 fm 11/29/01 3:19 PM Page vii
Contents vii
Quiz ................................................................................................................56
Quiz Answers ..................................................................................................57
Activities ..........................................................................................................58
HOUR 5 Going with the Flow 59
Switching Flow ....................................................................................................60
TheifStatement ............................................................................................60
Using the elseClause with the ifStatement ..............................................61
Using the elseifClause with the ifStatement ..........................................62
TheswitchStatement ....................................................................................63
Using the ?Operator ......................................................................................64
Loops ....................................................................................................................65
ThewhileStatement ......................................................................................65
Thedo...whileStatement ..........................................................................66
TheforStatement ..........................................................................................67
Breaking Out of Loops with the breakStatement ........................................68
Skipping an Iteration with the continueStatement ......................................70
Nesting Loops ..................................................................................................71
Codeblocks and Browser Output ..........................................................................72
Summary ..............................................................................................................73
Q&A ......................................................................................................................74
Workshop ..............................................................................................................74
Quiz ................................................................................................................74
Quiz Answers ..................................................................................................75
Activities ..........................................................................................................75
HOUR 6 Functions 77
What Is a Function? ..............................................................................................78
Calling Functions ..................................................................................................78
Defining a Function ..............................................................................................80
Returning Values from User-Defined Functions ..................................................81
Dynamic Function Calls ......................................................................................83
Variable Scope ......................................................................................................83
Accessing Variables with the globalStatement ............................................85
Saving State Between Function Calls with the staticStatement ....................87
More About Arguments ........................................................................................89
Setting Default Values for Arguments ............................................................89
Passing References to Variables to Functions ................................................91
Creating Anonymous Functions ..........................................................................93
Testing for Function Existence ............................................................................94
Summary ..............................................................................................................96
Q&A ......................................................................................................................96
Workshop ..............................................................................................................96
00 fm 11/29/01 3:19 PM Page viii
viii Sams Teach Yourself PHP in 24 Hours
Quiz ................................................................................................................96
Quiz Answers ..................................................................................................97
Activities ..........................................................................................................98
HOUR 7 Arrays 99
What Is an Array? ................................................................................................99
Creating Arrays ..................................................................................................100
Defining Arrays with the array()Construct ..............................................101
Defining or Adding to Arrays with the Array Identifier ..............................101
Associative Arrays ..............................................................................................102
Defining Associative Arrays with the array()Construct ..........................102
Directly Defining or Adding to an Associative Array ..................................103
Multidimensional Arrays ....................................................................................103
Accessing Arrays ................................................................................................105
Getting the Size of an Array ..........................................................................105
Looping through an Array ............................................................................106
Looping through an Associative Array ..........................................................107
Outputting a Multidimensional Array ..........................................................108
Manipulating Arrays ..........................................................................................110
Joining Two Arrays with array_merge() ..................................................110
Adding Multiple Variables to an Array with array_push() ......................110
Removing the First Element of an Array with array_shift() ................111
Slicing Arrays with array_slice() ..........................................................112
Sorting Arrays ....................................................................................................112
Sorting Numerically Indexed Arrays with sort() ....................................112
Sorting an Associative Array by Value with asort() ................................113
Sorting an Associative Array by Key with ksort() ....................................114
Functions Revisited ............................................................................................114
Summary ............................................................................................................116
Q&A ....................................................................................................................117
Workshop ............................................................................................................117
Quiz ..............................................................................................................117
Quiz Answers ................................................................................................118
Activities ........................................................................................................118
HOUR 8 Objects 119
What Is an Object? ............................................................................................120
Creating an Object ..............................................................................................120
Object Properties ................................................................................................121
Object Methods ..................................................................................................122
An Example ........................................................................................................125
Defining the Class’s Properties ....................................................................125
Creating a Constructor ..................................................................................125
00 fm 11/29/01 3:19 PM Page ix
Contents ix
TheaddRow()Method ................................................................................126
TheaddRowAssocArray()Method ............................................................126
Theoutput()Method ................................................................................127
Bringing It All Together ................................................................................127
What’s Missing? ............................................................................................129
Why a Class? ................................................................................................129
Inheritance ..........................................................................................................130
Overriding the Method of a Parent Class ......................................................131
Calling an Overridden Method ......................................................................132
Inheritance:An Example ....................................................................................133
Defining HTMLTable’s Properties ................................................................133
Creating the Constructor ..............................................................................134
ThesetCellpadding()Method ................................................................134
TheOutput() Method ................................................................................134
TheTableandHTMLTableClasses in Their Entirety ................................135
Why Use Inheritance? ..................................................................................137
Testing Classes and Objects ..............................................................................138
Finding the Class of an Object ......................................................................138
Finding the Family of an Object ..................................................................139
Checking for Class and Method Existence ..................................................141
Storing and Retrieving Objects ..........................................................................141
Summary ............................................................................................................143
Q&A ....................................................................................................................143
Workshop ............................................................................................................144
Quiz ..............................................................................................................144
Quiz Answers for Hour 8 ..............................................................................144
Activities ........................................................................................................146
PART III Working with PHP 147
HOUR 9 Working with Forms 149
Predefined Variables ..........................................................................................150
A Script to Acquire User Input ..........................................................................151
Accessing Form Input with User Defined Arrays ..............................................153
Accessing Form Input with Built-In Arrays ......................................................155
Distinguishing Between GET and POST Transactions ......................................156
Combining HTML and PHP Code
on a Single Page ..............................................................................................157
Using Hidden Fields to Save State ....................................................................159
Redirecting the User ..........................................................................................161
File Upload Forms and Scripts ..........................................................................163
Summary ............................................................................................................166
Description:This is not a PHP programming book. This book will teach you about the very basics of PHP syntax and structure and while it will not get you anywhere near proficiency, it will give you a very solid, albeit simple, foundation from which to continue. With literally thousands of included functions and