Zend PHP Certification Study Guide - PHP Ir MySQL

Transcription

00 7090 fm7/16/048:45 AMPage i

00 7090 fm7/16/048:45 AMPage iiZend PHP Certification Study GuideCopyright 2005 by Sams PublishingAcquisitions EditorShelley JohnstonAll rights reserved. No part of this book shall be reproduced, storedin a retrieval system, or transmitted by any means, electronic,mechanical, photocopying, recording, or otherwise, without writtenpermission from the publisher. No patent liability is assumed withrespect to the use of the information contained herein. Althoughevery precaution has been taken in the preparation of this book, thepublisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the useof the information contained herein.Development EditorDamon JordanInternational Standard Book Number: 0-672-32709-0IndexerChris BarrickLibrary of Congress Catalog Card Number: 2004093764Printed in the United States of AmericaFirst Printing: July 200407 06 05 044 3 2 1TrademarksAll terms mentioned in this book that are known to be trademarksor service marks have been appropriately capitalized. SamsPublishing cannot attest to the accuracy of this information. Use of aterm in this book should not be regarded as affecting the validity ofany trademark or service mark.Warning and DisclaimerEvery effort has been made to make this book as complete and asaccurate as possible, but no warranty or fitness is implied.The information provided is on an “as is” basis.Bulk SalesSams Publishing offers excellent discounts on this book whenordered in quantity for bulk purchases or special sales. For moreinformation, please contactU.S. Corporate and Government or sales outside of the U.S., please contactInternational comManaging EditorCharlotte ClappProject EditorGeorge E. NedeffCopy EditorRhonda Tinch-MizeProofreaderLeslie JosephTechnical EditorSara GolemonPublishing CoordinatorVanessa EvansMultimedia DeveloperDan ScherfBook DesignerGary AdairPage LayoutKelly Maish

00 7090 fm7/16/048:45 AMPage iiiContents at a GlanceIntroduction 11 The Basics of PHP 52 Object-Oriented PHP 353 PHP and the Web 494 Arrays 615 Strings and Regular Expressions 896 File Manipulation 1057 Managing Dates and Times 1158 Managing Email 1279 PHP and Databases 14510 Stream and Network Programming 15711 Security 17712 Debugging and Performance 18513 Getting Ready for the Certification Exam 201Practice Exam Questions 209Glossary 215Index 225

00 7090 fm7/16/048:45 AMPage ivTable of ContentsIntroduction1What Does This Guide Cover? 1How Is the Guide Organized? 2Other Resources You Might Want to Consult 31 The Basics of PHP 5Terms You’ll Need to Understand 5Techniques You’ll Need to Master 6Language and Platform 6Getting Started 6The Special ? ? Tags 8Scripts and Files 8Manipulating Data 9Numeric Values 9String Values 10Boolean Values 11Arrays 11Objects 11The NULL Data Type 11Resources 12Identifiers, Constants, and Variables 12Variables 12Variable Substitution in Strings 13Statements 13Constants 14Operators 14The Assignment Operator 14Arithmetic Operators 15Bitwise Operators 16Error-control Operators 16String Operators 17Comparison Operators 17Logical Operators 18Typecasting 19

00 7090 fm7/16/048:45 AMPage vCombined Assignment Operators 19Combining Operations: Operator Precedence andAssociativity 19Conditional Structures 21Alternative if-then-else Syntax 22Short-form if-then-else 22The case Statement 23Iteration and Loops 25The while Structure 25The do-while Structure 26The for Loop 26Continuing a Loop 28Functions and Constructs 28Functions and Variable Scope 30Functions with Variable Parameters 31Variable Variables and Variable Functions 32Exam Prep Questions 332 Object-Oriented PHP 35Terms You’ll Need to Understand 35Techniques You’ll Need to Master 36Getting Started 36Instantiating a Class: Objects 37Classes as Namespaces 37Objects and References 38Implementing Inheritance 42Magic Functions: Serializing Objects 44Exam Prep Questions 453 PHP and the Web 49Terms You’ll Need to Understand 49Techniques You’ll Need to Master 49Server-side Versus Client-side 50HTML Forms 51Cookies 54Sessions 56Exam Prep Questions 57

00 7090 fmvi7/16/048:45 AMPage viContents4 Arrays61Terms You’ll Need to Understand 61Techniques You’ll Need to Master 62Creating Arrays 62Using the Array Operator 63Counting the Number of Elements inan Array 65Assigning Values from an Array to MultipleVariables 65Multidimensional Arrays 66Navigating Arrays 68Using foreach 69Using the Internal Pointer 70Using a Callback 71Manipulating Keys 72Checking if an Element Exists 73Changing the Array of Keys 74Sorting an Array by Its Keys 74Manipulating Arrays 76Sorting Multidimensional Arrays 78Randomizing Arrays 81Merging Arrays 82Intersection and Difference 84Serializing Arrays 85Exam Prep Questions 865 Strings and Regular Expressions 89Terms You’ll Need to Understand 89Techniques You’ll Need to Master 89Comparing Strings 89Comparison with and 90Using strcmp and Friends 91Matching Portions of Strings 92Formatting Strings 93printf Formats 93printf() Family Functions 95

00 7090 fm7/16/048:45 AMPage viiContentsExtracting Data from Strings 95Extracting Substrings by Offset 96Extracting Formatted Data 96Modifying Strings 97Modifying Substrings by Offset 97Replacing Substrings 97Regular Expressions 98Basic PCRE Syntax 98Extracting Data with Regular Expressions 100Pattern Replacement with RegularExpressions 101Splitting Strings into Components 101Exam Prep Questions 1026 File Manipulation 105Techniques You’ll Need to Master 105Terms You’ll Need to Understand 105Opening Files 106Closing Files 107Reading from a File 107Writing to a File 108Determining Information About Files 109Manipulating Files on the Filesystem 110Copying, Deleting, and Moving Files 110Changing Ownership and Permissions 111Locking Files 111Miscellaneous Shortcuts 112file() 112readfile() 112file get contents() 113Exam Prep Questions 1137 Managing Dates and Times 115Terms You’ll Need to Understand 115Techniques You’ll Need to Master 115How PHP Handles Dates 115Getting the Current Time Stamp 117vii

00 7090 fmviii7/16/048:45 AMPage viiiContentsGetting a Date Array 117Formatting a Date String 119Getting a UNIX Time Stamp from a Date Array 123Getting A UNIX Time Stamp from a String 123Exam Prep Questions 1248 Managing Email 127Introduction 127Terms You’ll Need to Understand127Techniques You’ll Need to Master 127How Email Is Delivered 127MTA—Mail Transport Agent 128SMTP—Simple Mail Transport Protocol 128MX Records 128MUA—Mail User Agent 129SASL—Simple Authentication andSecurity Layer 129Other Emerging Technologies 129Preparing PHP 130If You Are Using PHP on UNIX 130If You Are Using PHP on Windows orNetware 131Sending Email 132Sending an Email to More Than OneRecipient 132Managing Email Headers 133The Cc: and Bcc: Headers 133The From: Header 133Setting the Subject 133Formatting an Email Message 133Plain-Text Emails 133Basic HTML Emails 134Attaching a File to a Message 135Attached Images for HTML Emails 137Using Extra Command-Line Parameters 139A Word About Email Delivery 139Further Reading 140Exam Prep Questions 141

00 7090 fm7/16/048:45 AMPage ixContents9 PHP and Databases 145Terms You’ll Need to Understand 146Techniques You’ll Need to Master 146“Databasics” 146Indices 147Writing Good Indices 147Primary Keys 148Foreign Keys and Relations 148Creating Tables or Adding and Removing Rows 149Inserting a Row 149Deleting Rows 149Retrieving Information from a Database 150Extracting Data from More Than One Table 150Aggregate Functions 151Sorting 152Transactions 153PHP and Databases 153There’s Date and Date 154Exam Prep Questions 15410 Stream and Network Programming 157Terms You’ll Need to Understand 157Techniques You’ll Need to Master 157php.ini Settings to Understand 157What Are File Wrappers? 158How Do You Choose Which File Wrapper IsUsed? 158What Built-In Wrappers Does PHPProvide? 159Not All Wrappers Are Created Equal 160Using a File Wrapper 162Correctly Detecting Line Endings 162Closing a File Wrapper 162Other Functions That Work with FileWrappers 163Introducing Streams 163What Is Stream Metadata? 163Pipelines 165What Is the Stream Transport? 165ix

00 7090 fmx7/16/048:45 AMPage xContentsWhat Is the Stream Context? 165How Do Streams Affect Me? 166Connecting to Remote Hosts Using Sockets 166When Should I Use a Socket Instead of a FileWrapper? 166What Network Transports Does PHPSupport? 167How Do I Open a Socket? 167Persistent Sockets 168Timeouts When Opening a Socket 168How Do I Use a Socket? 169Blocking Mode 169Read/Write Timeouts 170Closing a Socket 171Further Reading 172Exam Prep Questions 17211 Security177Terms You’ll Need to Understand 177Techniques You’ll Need to Master 177Data Filtering 178Register Globals 178SQL Injection 179Command Injection 180Cross-Site Scripting 180Shared Hosting 180Exam Prep Questions 18112 Debugging and Performance 185Terms You’ll Need to Understand 185Techniques You’ll Need to Master 185Coding Standards 186Flattening if Statements 187Splitting Single Commands AcrossMultiple Lines 188Concatenation Versus Substitution 188Choose Your Opening Tags Carefully 189One Equal,Two Equals,Three Equals 189There’s Equal and Equal 190Testing for Resource Allocation 190

00 7090 fm7/16/048:45 AMPage xiContentsTernary Operators and if Statements 191Logging and Debugging 192Using Debuggers 193Optimizing Performance 193Hardware Issues 193Web Server Issues 194Avoid Overkill 194Zip It Up 195Database Optimizations 195Keep Your Code Simple 196Caching Techniques 196Bytecode Caches 196Exam Prep Questions 19713 Getting Ready for theCertification Exam 201What the Exam Tests 201How to Register 201Registration via Pearson VUE Call Center 202Registration via the Person VUE Website 202Registration at the Test Center 202What to Expect at the Test Center 202How the Exam Works 203Exam Instructions 203NDA (NONDISCLOSUREAGREEMENT) 203Viewing Backward and Forward 204Reviewing Your Answers 204Your Comments 204What Kinds of Questions Are Asked? 204Single Choice Questions 204Multiple Choice Questions 205Fill in the Blanks Questions 206Open Questions 207Practice Exam Questions 209Glossary 215Index 225xi

00 7090 fm7/16/048:45 AMPage xiiAbout the AuthorsStuart Herbert has designed and implemented solutions for major companies such asEurostar,Vodafone, and HP, and has been the lead maintainer of the Generic NQSproject since 1994. A former systems manager with Orange UK, Stuart is currently oneof the developers for Gentoo Linux, where he looks after Gentoo’s installer for webbased packages.Daniel Kushner is the director of training and certification at Zend Technologies. Asdirector of training and certification, Daniel is responsible for the Zend PHPCertification program. In addition to designing the certification program, he developedthe Zend PHP Training program, which provides the necessary study guide and classesto help PHP developers become Zend PHP certified. As part of the program, Daniel alsoinitiates and maintains business relationships and partnerships with worldwide PHPtraining facilities. Prior to Zend Technologies, Daniel was a senior software engineer atDynamicLogic, responsible for developing integrated research recruitment solutions usedin name brand websites including Yahoo!, AOL, and Lycos. Previously, he was a PHPfreelancer, developing front and backend web applications, including e-commerce integration, member services and personalization, auction management, email delivery systems, and online file manipulation services for companies such as MTV, Arista,ViacomOutdoor, Accuweather, and Dell Computer Corporation.While freelancing, Daniel wasalso a PHP training instructor, where he worked with developers from highly acclaimeduniversities such as Harvard and Columbia and with companies such as Google, The NewYork Times, and the American Museum of Natural History.George Schlossnagle is a principal at OmniTI Computer Consulting, a Marylandbased tech company specializing in high-volume web and email systems. Before joiningOmniTI, George lead technical operations at several high-profile community websites,where he developed experience managing PHP in very large enterprise environments.George is a frequent contributor to the PHP community. His work can be found in thePHP core, as well as in the PEAR and PECL extension repositories. George also writesprolifically on PHP. He is the author of Advanced PHP Programming (Developers Library,2004), as well as numerous articles for both print and online journals. He served as asubject matter expert for the Zend PHP Certification exam.Chris Shiflett is a frequent contributor to the PHP community and one of the leadingsecurity experts in the field. His solutions to security problems are often used as pointsof reference, and these solutions are showcased in his talks at conferences such asApacheCon and the O’Reilly Open Source Convention and his articles in publicationssuch as PHP Magazine and php architect. “Security Corner,” his monthly column forphp architect, is the industry’s first and foremost PHP security column. Chris is the authorof the HTTP Developer’s Handbook (Sams Publishing) and PHP Security (O’Reilly andAssociates). In order to help bolster the strength of the PHP community, he is also leading an effort to create a PHP community site at PHPCommunity.org. Chris is also asubject matter expert for the Zend PHP Certification Exam.

00 7090 fm7/16/048:45 AMPage xiiiBjörn Schotte is a German PHP veteran. He co-founded the first German-speakingportal for PHP topics in early 1999, co-organized the first worldwide PHP conference,and has been editor-in-chief of PHP Magazin since 2001. He also co-foundedThinkPHP, Germany’s No. 1 PHP solution company dealing mainly with large PHPapplications for clients such as HypoVereinsbank, Sixt, Lycos Europe, E.ON, CapGemini, Ernst & Young, and others. His compa

Certification program.In addition to designing the certification program,he developed the Zend PHP Training program,which provides the necessary study guide and classes to help PHP developers become Zend PHP certified.As part of the program,Daniel also