Table Of Contentphp|architect’s
php|architect’s
Zend PHP 5 Certification
ZEND PHP 5
Study Guide
Certification
Zend's new PHP 5 Certification Exam represent an excellent tool for professional
PHP developers who want to distinguish themselves in their field.
php|architect's Zend PHP 5 Certification Study Guide, edited and produced by
e
the publishers of php|architect magazine, provides the most comprehensive and d
thorough preparation tool for developers who wish to take the exam. ui STUDY GUIDE
G
This book provides complete coverage of every topic that is part of the exam, n
o
including: i
t
a
c
fi
✔ PHP Basics rti Second Edition
e
✔ Functions C
✔ Arrays 5
✔ Strings and Patterns P
H
✔ Web Programming
P
✔ Object Oriented Programming
d
✔ Database Programming n
e
✔ Object-oriented Design Z
✔ XML and Web Services s
’
✔ Security ct
✔ Streams and Network Programming e
t
i
✔ Differences Between PHP 4 and 5 h
c
r
a
|
p
NEW IN THE SECOND EDITION: advanced database topics (PDO/mysqli), errata, h
p
new examples, and much, much more!
y
e
s
m
MSRP $32.99 USD
a
R
B.
From the publishers of /
k
fi
a
h
S
Shelve under PHP/Web Development/Internet Programming D. Davey Shafik and Ben Ramsey
7.50 x 9.25 7.50 x 9.25
.571
Licensed to:
Philippe Dellaert
[email protected]
User #IL-05391-09
Zend PHP 5 Certification
g)
or
ert.
Study Guide a
ell
d
@
e
p
p
2ndEdition hili
p
ert (
a
ell
D
byDaveyShafik e
p
withBenRamsey p
hili
P
9 -
0
1-
9
3
5
0
L-
o I
d t
e
s
n
e
c
Li
php|architect’sZendPHP5CertificationGuide
ContentsCopyright©2006–2007DavidShafikandBenRamsey–AllRightsReserved
Bookandcoverlayout,designandtextCopyright©2004-2007MarcoTabini&Associates,Inc.–AllRightsReserved
FirstEdition:October2006
SecondEdition:December2007
ISBN:0-9738621-4-9
ProducedinCanada
PrintedintheUnitedStates
Nopartofthisbookmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformor
bymeanswithoutthepriorwrittenpermissionofthepublisher,excetinthecaseofbriefquotations
embeddedincriticalreviewsorarticles. g)
or
Disclaimer ert.
a
Althougheveryefforthasbeenmadeinthepreparationofthisbooktoensuretheaccuracyofthe ell
d
informationcontainedtherein,thisbookisprovided“as-is”andthepublisher,theauthor(s),theirdis- @
e
tributorsandretailers,aswellasallaffiliated,relatedorsubsidiarypartiestakenoresponsibilityforany p
p
inaccuracyandanyandalldamagescaused,eitherdirectlyorindirectly,bytheuseofsuchinforma- hili
tmioenn.tWioenehdavineethnedbeaovookubryedthteoappropproeprlryiaptreouvisdeeotfrcaadpeimtaalsr.kHinofworemveart,iwoneocannanlloctogmuapraanniteeseatnhdepacrocudruacctys ert (p
a
ofsuchinformation. ell
D
e
MarcoTabini&Associates,TheMTAlogo,php|architect,thephp|architectlogo,NanoBookandthe pp
NanoBooklogoaretrademarksorregisteredtrademarksofMarcoTabini&Associates,Inc. hili
P
9 -
0
1-
Writtenby DaveyShafik 9
3
BenRamsey 05
Publishedby M2To8arBroconomtoTb,aaObyiNnAiMv&e3.AHss1oBc7iates,Inc. ed to IL-
s
Canada n
e
c
Li
(416)630-6202/(877)630-6202
[email protected]/www.phparch.com
Editedby SeanCoates
TechnicalReviewers DerickRethans
PaulReinheimer
LayoutandDesign ArbiArzoumani
ManagingEditor ElizabethNaramore
FinanceandResourceManagement EmanuelaCorso
g)
or
ert.
a
ell
d
@
e
p
p
hili
p
ert (
a
ell
D
e
p
p
hili
P
9 -
0
1-
9
3
5
0
L-
o I
d t
e
s
n
e
c
Li
g)
or
ert.
a
ell
d
@
e
p
p
hili
p
ert (
a
ell
D
e
p
p
hili
P
9 -
0
1-
9
3
5
0
L-
o I
d t
e
s
n
e
c
Li
Contents
g)
Foreword xv or
ert.
a
HowToUseThisBook xvii ell
d
@
e
Chapter1—PHPBasics 3 p
p
Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 hili
p
SourceFilesandPHPTags . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ert (
a
NewlineCharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 ell
D
AnatomyofaPHPScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 e
p
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 hilip
Whitespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 P
CodeBlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 09 -
1-
LanguageConstructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 9
3
5
DataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 0
L-
NumericValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 o I
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 ed t
s
n
Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 e
c
CompoundDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Li
OtherDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
ConvertingBetweenDataTypes . . . . . . . . . . . . . . . . . . . . . . . 11
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
VariableVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
DeterminingIfaVariableExists. . . . . . . . . . . . . . . . . . . . . . . . 14
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
vi ” CONTENTS
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
ArithmeticOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
TheStringConcatenationOperator . . . . . . . . . . . . . . . . . . . . . 17
BitwiseOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
AssignmentOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
ReferencingVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
ComparisonOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
LogicalOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
OtherOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
OperatorPrecedenceandAssociativity . . . . . . . . . . . . . . . . . . . 25
ControlStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 g)
or
ConditionalStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 ert.
a
IterativeConstructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 ell
d
BreakingandContinuing . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 @
e
ErrorsandErrorManagement . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 pp
TypesofErrors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 hili
p
ErrorReporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 ert (
a
HandlingErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 ell
D
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 e
p
p
Chapter2—Functions 37 Phili
BasicSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 9 -
0
ReturningValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 91-
3
VariableScope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 05
L-
PassingArguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 o I
Variable-lengthArgumentLists . . . . . . . . . . . . . . . . . . . . . . . . 42 d t
e
s
PassingArgumentsbyReference . . . . . . . . . . . . . . . . . . . . . . . 43 n
e
c
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Li
Chapter3—Arrays 47
ArrayBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
PrintingArrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Enumerativevs.Associative . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Multi-dimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
UnravellingArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
CONTENTS ” vii
ArrayOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
ComparingArrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Counting,SearchingandDeletingElements . . . . . . . . . . . . . . . . 53
FlippingandReversing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
ArrayIteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
TheArrayPointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
AnEasierWaytoIterate . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
PassiveIteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
SortingArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
OtherSortingOptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
TheAnti-Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 g)
or
ArraysasStacks,QueuesandSets . . . . . . . . . . . . . . . . . . . . . . . . . 67 ert.
a
SetFunctionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ell
d
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 @
e
p
p
Chapter4—StringsAndPatterns 73 hili
p
StringBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 ert (
VariableInterpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 a
ell
TheHeredocSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 D
e
p
EscapingLiteralValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 p
DeterminingtheLengthofaString. . . . . . . . . . . . . . . . . . . . . . 76 Phili
TransformingaString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 9 -
0
UsingStringsasArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 91-
3
Comparing,SearchingandReplacingStrings. . . . . . . . . . . . . . . . . . . 78 05
L-
SimpleSearchingFunctionality . . . . . . . . . . . . . . . . . . . . . . . . 79 o I
MatchingAgainstaMask . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 d t
e
s
SimpleSearchandReplaceOperations . . . . . . . . . . . . . . . . . . . 81 n
e
c
ExtractingSubstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Li
FormattingStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
FormattingNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
FormattingCurrencyValues . . . . . . . . . . . . . . . . . . . . . . . . . . 85
GenericFormatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
ParsingFormattedInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Perl-compatibleRegularExpressions . . . . . . . . . . . . . . . . . . . . . . . 89
viii ” CONTENTS
Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Sub-Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
MatchingandExtractingStrings . . . . . . . . . . . . . . . . . . . . . . . 92
UsingPCREtoReplaceStrings . . . . . . . . . . . . . . . . . . . . . . . . 94
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Chapter5—WebProgramming 97
AnatomyofaWebPage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
FormsandURLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 g)
GETandURLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 ert.or
UsingPOST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 a
ell
WhenYouDon’tKnowHowDataIsSent . . . . . . . . . . . . . . . . . . 101 @d
ManagingFileUploads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 pe
p
GETorPOST? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 hili
p
HTTPHeaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 ert (
Redirection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 a
ell
Compression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 D
e
p
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 p
Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Phili
Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 9 -
0
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 91-
3
5
0
Chapter6—ObjectOrientedProgramminginPHP 113 L-
o I
OOPFundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 d t
e
DeclaringaClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 s
n
e
InstantiatinganObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 c
Li
ClassInheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
ClassMethodsandProperties. . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
DeclaringandAccessingProperties . . . . . . . . . . . . . . . . . . . . . 122
Constants,StaticMethodsandProperties. . . . . . . . . . . . . . . . . . . . . 123
CONTENTS ” ix
ClassConstants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Interfaces&AbstractClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
DeterminingAnObject’sClass . . . . . . . . . . . . . . . . . . . . . . . . 128
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
TheBasicExceptionClass . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
ThrowingExceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
LazyLoading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
g)
Chapter7—DatabaseProgramming 141 ert.or
AnIntroductiontoRelationalDatabasesandSQL . . . . . . . . . . . . . . . . 141 a
ell
Indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 @d
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 pe
p
SQLDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 hili
p
CreatingDatabasesandTables . . . . . . . . . . . . . . . . . . . . . . . . 145 ert (
CreatingIndicesandRelationships . . . . . . . . . . . . . . . . . . . . . 146 a
ell
DroppingObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 D
e
p
AddingandManipulatingData . . . . . . . . . . . . . . . . . . . . . . . . 148 p
RemovingData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Phili
RetrievingData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 9 -
0
SQLJoins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 91-
3
OuterJoins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 05
L-
AdvancedDatabaseTopics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 o I
Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 d t
e
s
PreparedStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 n
e
c
WorkingWithDatabases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Li
PHPDataObjects(PDO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
MySQLImprovedExtension(mysqli) . . . . . . . . . . . . . . . . . . . . 162
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Chapter8—ElementsofObject-orientedDesign 171
DesignPatternTheory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
TheSingletonPattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Description:First off, this so-called Study Guide for the PHP 5 Certification is completely inadequate for anyone serious about taking the exam.
Ramsey and Shafik's introduction to PHP is the perfect primer for someone new to PHP from any background. It assumes a little experience in programming and introduces