Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 00 7090 fm 7/16/04 8:45 AM Page i Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Zend PHP Certification Study Guide Copyright © 2005 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omis- sions. Nor is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-32709-0 Library of Congress Catalog Card Number: 2004093764 Printed in the United States of America First Printing: July 2004 07060504 4321 Trademarks 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 accuracy of this information. Use of a term in this book should not be regarded 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 infor- mation provided is on an “as is” basis. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales 1-317-428-3341 international@pearsontechgroup.com Acquisitions Editor Shelley Johnston Development Editor Damon Jordan Managing Editor Charlotte Clapp Project Editor George E. Nedeff Copy Editor Rhonda Tinch-Mize Indexer Chris Barrick Proofreader Leslie Joseph Technical Editor Sara Golemon Publishing Coordinator Vanessa Evans Multimedia Developer Dan Scherf Book Designer Gary Adair Page Layout Kelly Maish 00 7090 fm 7/16/04 8:45 AM Page ii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Contents at a Glance Introduction 1 1 The Basics of PHP 5 2 Object-Oriented PHP 35 3 PHP and the Web 49 4 Arrays 61 5 Strings and Regular Expressions 89 6 File Manipulation 105 7 Managing Dates and Times 115 8 Managing Email 127 9 PHP and Databases 145 10 Stream and Network Programming 157 11 Security 177 12 Debugging and Performance 185 13 Getting Ready for the Certification Exam 201 Practice Exam Questions 209 Glossary 215 Index 225 00 7090 fm 7/16/04 8:45 AM Page iii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Table of Contents Introduction 1 What Does This Guide Cover? 1 How Is the Guide Organized? 2 Other Resources You Might Want to Consult 3 1 The Basics of PHP 5 Terms You’ll Need to Understand 5 Techniques You’ll Need to Master 6 Language and Platform 6 Getting Started 6 The Special <?= ?> Tags 8 Scripts and Files 8 Manipulating Data 9 Numeric Values 9 String Values 10 Boolean Values 11 Arrays 11 Objects 11 The NULL Data Type 11 Resources 12 Identifiers, Constants, and Variables 12 Variables 12 Variable Substitution in Strings 13 Statements 13 Constants 14 Operators 14 The Assignment Operator 14 Arithmetic Operators 15 Bitwise Operators 16 Error-control Operators 16 String Operators 17 Comparison Operators 17 Logical Operators 18 Typecasting 19 00 7090 fm 7/16/04 8:45 AM Page iv Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Combined Assignment Operators 19 Combining Operations: Operator Precedence and Associativity 19 Conditional Structures 21 Alternative if-then-else Syntax 22 Short-form if-then-else 22 The case Statement 23 Iteration and Loops 25 The while Structure 25 The do-while Structure 26 The for Loop 26 Continuing a Loop 28 Functions and Constructs 28 Functions and Variable Scope 30 Functions with Variable Parameters 31 Variable Variables and Variable Functions 32 Exam Prep Questions 33 2 Object-Oriented PHP 35 Terms You’ll Need to Understand 35 Techniques You’ll Need to Master 36 Getting Started 36 Instantiating a Class: Objects 37 Classes as Namespaces 37 Objects and References 38 Implementing Inheritance 42 Magic Functions: Serializing Objects 44 Exam Prep Questions 45 3 PHP and the Web 49 Terms You’ll Need to Understand 49 Techniques You’ll Need to Master 49 Server-side Versus Client-side 50 HTML Forms 51 Cookies 54 Sessions 56 Exam Prep Questions 57 00 7090 fm 7/16/04 8:45 AM Page v Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. vi Contents 4Arrays 61 Terms You’ll Need to Understand 61 Techniques You’ll Need to Master 62 Creating Arrays 62 Using the Array Operator 63 Counting the Number of Elements in an Array 65 Assigning Values from an Array to Multiple Variables 65 Multidimensional Arrays 66 Navigating Arrays 68 Using foreach 69 Using the Internal Pointer 70 Using a Callback 71 Manipulating Keys 72 Checking if an Element Exists 73 Changing the Array of Keys 74 Sorting an Array by Its Keys 74 Manipulating Arrays 76 Sorting Multidimensional Arrays 78 Randomizing Arrays 81 Merging Arrays 82 Intersection and Difference 84 Serializing Arrays 85 Exam Prep Questions 86 5 Strings and Regular Expressions 89 Terms You’ll Need to Understand 89 Techniques You’ll Need to Master 89 Comparing Strings 89 Comparison with == and === 90 Using strcmp and Friends 91 Matching Portions of Strings 92 Formatting Strings 93 printf Formats 93 printf() Family Functions 95 00 7090 fm 7/16/04 8:45 AM Page vi Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. vii Contents Extracting Data from Strings 95 Extracting Substrings by Offset 96 Extracting Formatted Data 96 Modifying Strings 97 Modifying Substrings by Offset 97 Replacing Substrings 97 Regular Expressions 98 Basic PCRE Syntax 98 Extracting Data with Regular Expressions 100 Pattern Replacement with Regular Expressions 101 Splitting Strings into Components 101 Exam Prep Questions 102 6 File Manipulation 105 Techniques You’ll Need to Master 105 Terms You’ll Need to Understand 105 Opening Files 106 Closing Files 107 Reading from a File 107 Writing to a File 108 Determining Information About Files 109 Manipulating Files on the Filesystem 110 Copying, Deleting, and Moving Files 110 Changing Ownership and Permissions 111 Locking Files 111 Miscellaneous Shortcuts 112 file() 112 readfile() 112 file_get_contents() 113 Exam Prep Questions 113 7 Managing Dates and Times 115 Terms You’ll Need to Understand 115 Techniques You’ll Need to Master 115 How PHP Handles Dates 115 Getting the Current Time Stamp 117 00 7090 fm 7/16/04 8:45 AM Page vii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. viii Contents Getting a Date Array 117 Formatting a Date String 119 Getting a UNIX Time Stamp from a Date Array 123 Getting A UNIX Time Stamp from a String 123 Exam Prep Questions 124 8 Managing Email 127 Introduction 127 Terms You’ll Need to Understand 127 Techniques You’ll Need to Master 127 How Email Is Delivered 127 MTA—Mail Transport Agent 128 SMTP—Simple Mail Transport Protocol 128 MX Records 128 MUA—Mail User Agent 129 SASL—Simple Authentication and Security Layer 129 Other Emerging Technologies 129 Preparing PHP 130 If You Are Using PHP on UNIX 130 If You Are Using PHP on Windows or Netware 131 Sending Email 132 Sending an Email to More Than One Recipient 132 Managing Email Headers 133 The Cc: and Bcc: Headers 133 The From: Header 133 Setting the Subject 133 Formatting an Email Message 133 Plain-Text Emails 133 Basic HTML Emails 134 Attaching a File to a Message 135 Attached Images for HTML Emails 137 Using Extra Command-Line Parameters 139 A Word About Email Delivery 139 Further Reading 140 Exam Prep Questions 141 00 7090 fm 7/16/04 8:45 AM Page viii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ix Contents 9 PHP and Databases 145 Terms You’ll Need to Understand 146 Techniques You’ll Need to Master 146 “Databasics” 146 Indices 147 Writing Good Indices 147 Primary Keys 148 Foreign Keys and Relations 148 Creating Tables or Adding and Removing Rows 149 Inserting a Row 149 Deleting Rows 149 Retrieving Information from a Database 150 Extracting Data from More Than One Table 150 Aggregate Functions 151 Sorting 152 Transactions 153 PHP and Databases 153 There’s Date and Date 154 Exam Prep Questions 154 10 Stream and Network Programming 157 Terms You’ll Need to Understand 157 Techniques You’ll Need to Master 157 php.ini Settings to Understand 157 What Are File Wrappers? 158 How Do You Choose Which File Wrapper Is Used? 158 What Built-In Wrappers Does PHP Provide? 159 Not All Wrappers Are Created Equal 160 Using a File Wrapper 162 Correctly Detecting Line Endings 162 Closing a File Wrapper 162 Other Functions That Work with File Wrappers 163 Introducing Streams 163 What Is Stream Metadata? 163 Pipelines 165 What Is the Stream Transport? 165 00 7090 fm 7/16/04 8:45 AM Page ix Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... Kushner is the director of training and certification at Zend Technologies As director of training and certification, Daniel is responsible for the Zend PHP 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... column for php| architect, is the industry’s first and foremost PHP security column Chris is the author of the HTTP Developer’s Handbook (Sams Publishing) and PHP Security (O’Reilly and Associates) 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 a subject matter expert for the Zend PHP Certification. .. 7090 fm 7/16/04 8:45 AM Page xiii Björn Schotte is a German PHP veteran He co-founded the first German-speaking portal 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-founded ThinkPHP, Germany’s No 1 PHP solution company dealing mainly with large PHP applications for clients such as HypoVereinsbank, Sixt,... disproving those who say that PHP is not ready for the enterprise.With PHP 5, this trend is likely to continue even more rapidly As the scope of PHP widens, and as larger companies hire PHP personnel, they need to have a standard, reliable way of assessing one’s abilities and capabilities with PHP Moreover, the need for PHP developers to have an “official blessing” of their PHP skills is on the rise as... becomes more competitive.The Zend PHP Certification program is aimed at providing a solution for both these companies and developers by providing a standard, objective, and comprehensive measurement of one’s PHP skills This is exactly where this book comes into the picture Zend PHP Certification Study Guide has been written by some of the same people who designed the Zend PHP Certification It goes over all... an example.We’ll start with a file that we will call includefile .php: < ?php echo ‘You have included a file’; ?> Next, we’ll move on to mainfile .php: < ?php include ‘includefile .php ; echo ‘I should have included a file.’; ?> If you make sure to put both files in the same directory and execute mainfile .php, you will notice that includefile .php is included and executed, causing the text You have included... flexibility, PHP supports three different sets of tags: PHP opening (< ?php) and closing (?>) tags n n n n HTML-style tags ( . training and certification at Zend Technologies. As director of training and certification, Daniel is responsible for the Zend PHP Certification program. In addition to designing the certification. German-speaking portal 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-founded ThinkPHP, Germany’s No. 1 PHP solution. capabilities with PHP. Moreover, the need for PHP developers to have an “official blessing” of their PHP skills is on the rise as the market becomes more competitive.The Zend PHP Certification program