PHP 5 Recipes A Problem-Solution Approach Lee Babin, Nathan A. Good, Frank M. Kromann, Jon Stephens 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page i PHP 5 Recipes: A Problem-Solution Approach Copyright © 2005 by Lee Babin, Nathan A. Good, Frank M. Kromann, Jon Stephens All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN (pbk): 1-59059-509-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Chris Mills Technical Reviewer: Rob Kunkle Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Associate Publisher: Grace Wong Project Manager: Kylie Johnston Copy Edit Manager: Nicole LeClerc Copy Editor: Kim Wimpsett Assistant Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor and Artist: Van Winkle Design Group Proofreader: April Eddy Indexer: Broccoli Information Management Interior Designer: Van Winkle Design Group Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code section. 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page ii Contents at a Glance iii About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■ CHAPTER 1 Overview of PHP Data Types and Concepts . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 Overview of Classes, Objects, and Interfaces . . . . . . . . . . . . . . . . . . . 21 ■ CHAPTER 3 Performing Math Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 ■ CHAPTER 4 Working with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 ■ CHAPTER 5 Working with Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 ■ CHAPTER 6 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 ■ CHAPTER 7 Working with Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 ■ CHAPTER 8 Working with Dynamic Imaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 ■ CHAPTER 9 Using Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 ■ CHAPTER 10 Working with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 ■ CHAPTER 11 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 ■ CHAPTER 12 Understanding Web Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 ■ CHAPTER 13 Creating and Using Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 ■ CHAPTER 14 Working with Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 ■ CHAPTER 15 Using MySQL Databases in PHP 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 ■ CHAPTER 16 Communicating with Internet Services . . . . . . . . . . . . . . . . . . . . . . . . 597 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page iii 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page iv Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■ CHAPTER 1 Overview of PHP Data Types and Concepts . . . . . . . . . . . . . . . . 1 1-1. Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1-2. Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1-3. Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1-4. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1-5. Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1-6. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1-7. Project: Finding the Data Type of a Value . . . . . . . . . . . . . . . . . . . . . . . 10 1-8. Project: Discovering What Variables, Constants, Functions, Classes, and Interfaces Are Available . . . . . . . . . . . . . . . . . . 12 1-9. Getting Information About the Current Script . . . . . . . . . . . . . . . . . . . . 14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 ■ CHAPTER 2 Overview of Classes, Objects, and Interfaces . . . . . . . . . . . . . 21 Understanding Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2-1. Creating Instances Using Constructors . . . . . . . . . . . . . . . . . . . . . . . . . 24 2-2. Using Default Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2-3. Setting Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2-4. Controlling Access to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2-5. Using Static Members and the self Keyword . . . . . . . . . . . . . . . . . . . . 33 2-6. Using Class Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2-7. Extending Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2-8. Using Abstract Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 50 2-9. Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2-10. Using Class Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2-11. Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Getting Information About Classes and Objects . . . . . . . . . . . . . . . . . . . . . . 61 Using Class and Object Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 v 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page v 2-12. Checking for the Existence of Classes and Interfaces Using class_exists() and interface_exists() . . . . . . . . . . . . . . 63 2-13. Listing Methods and Interfaces Using get_class_methods() . . . . . . . 65 2-14. Obtaining Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 2-15. Determining Whether an Object Is an Instance of a Particular Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 2-16. Listing Currently Loaded Interfaces and Classes . . . . . . . . . . . . . . . 69 Using the Class Reflection API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 2-17. Obtaining a Dump of the Reflection API . . . . . . . . . . . . . . . . . . . . . . . 73 2-18. Performing Dynamic Class Instantiation . . . . . . . . . . . . . . . . . . . . . . . 76 2-19. Using the Reflection API to Deconstruct the Shape Class . . . . . . . . 77 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 ■ CHAPTER 3 Performing Math Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3-1. Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3-2. Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 3-3. Logarithms and Exponents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 3-4. Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 3-5. Formatting of Numeric Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 3-6. Math Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 3-7. A Static Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 ■ CHAPTER 4 Working with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 4-1. Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 4-2. Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 4-3. Creating Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 4-4. Using Array Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 4-5. Initializing an Array As a Range or Sequence of Values . . . . . . . . . . 124 Outputting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 4-6. Outputting an Array As a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 4-7. Outputting Using array_values() and array_keys() for Backward Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 4-8. Outputting an Array As a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Adding New Elements to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 4-9. Adding an Element to the End of an Array . . . . . . . . . . . . . . . . . . . . . 131 4-10. Appending One Array to Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 4-11. Comparing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 ■ CONTENTSvi 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page vi 4-12. Adding an Element to the Beginning of an Array . . . . . . . . . . . . . . . 137 4-13. Inserting New Values at an Arbitrary Point in an Indexed Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Getting and Setting the Size of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 4-14. Counting Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 4-15. Setting an Array’s Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Traversing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 4-16. Looping Through an Associative Array Using foreach . . . . . . . . . . . 144 4-17. Looping Through a Compact Indexed Array Using for and count() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 4-18. Looping Through a Sparse Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Removing Elements from Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 4-19. Removing the First or Last Element from an Array . . . . . . . . . . . . . 148 4-20. Removing One or More Arbitrary Array Elements . . . . . . . . . . . . . . 150 4-21. Extracting a Portion of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 4-22. Extracting Values from Arrays with extract() . . . . . . . . . . . . . . . . . . 154 4-23. Extracting Values from an Array Using list() . . . . . . . . . . . . . . . . . . . 156 4-24. Combining Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 4-25. Obtaining Array Keys and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 4-26. Working with Unique Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 4-27. Getting and Displaying Counts of Array Values . . . . . . . . . . . . . . . . 161 Finding and Working with Array Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 4-28. Determining Whether an Element Is in an Array . . . . . . . . . . . . . . . 163 4-29. Testing for the Existence of a Key in an Array . . . . . . . . . . . . . . . . . 164 4-30. Obtaining Array Keys with a Given Value . . . . . . . . . . . . . . . . . . . . . 165 4-31. Finding the Greatest and Least Values in an Array . . . . . . . . . . . . . 166 4-32. Finding the Sum and Average of the Values in an Array . . . . . . . . 168 Applying Functions to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 4-33. Applying Functions to Array Elements Using array_walk() . . . . . . 170 4-34. Applying Functions to Array Elements Using array_map() . . . . . . . 173 4-35. Filtering Arrays Using array_filter() . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 4-36. Sorting an Array by Its Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 4-37. Sorting an Array by Its Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 4-38. Reversing an Array Using arsort() . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 4-39. Reversing an Array Using krsort() . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 4-40. Reversing an Array Using array_reverse() . . . . . . . . . . . . . . . . . . . . 182 4-41. Randomizing an Array Using shuffle(), kshuffle(), and array_rand() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 4-42. Sorting an Array Using Comparison Functions . . . . . . . . . . . . . . . . 184 ■ CONTENTS vii 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page vii 4-43. Sorting Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 4-44. Sorting Multiple Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Finding Permutations and Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 4-45. Finding All Permutations of an Array’s Elements . . . . . . . . . . . . . . . 190 4-46. Finding All Combinations of an Array’s Elements . . . . . . . . . . . . . . 193 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 ■ CHAPTER 5 Working with Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Overview of PHP 5’s Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . 198 Displaying Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 5-1. Displaying Human-Readable Dates and Times . . . . . . . . . . . . . . . . . 201 5-2. Displaying Arbitrary Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . 204 5-3. Converting Human-Readable Dates Into Unix Timestamps Using strtotime() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 5-4. Finding the Date for a Weekday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 5-5. Getting the Day and Week of the Year . . . . . . . . . . . . . . . . . . . . . . . . . 211 5-6. Determining Whether a Given Year Is a Leap Year . . . . . . . . . . . . . . . 213 5-7. Getting Times and Dates of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 5-8. Setting Time Zones and GMT/UTC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 5-9. Displaying Times and Dates in Other Languages . . . . . . . . . . . . . . . 219 5-10. Generating Localized GMT/UTC Time and Date Strings . . . . . . . . . 224 5-11. Obtaining the Difference Between Two Dates . . . . . . . . . . . . . . . . . 225 5-12. Project: Constructing and Using a Date Class . . . . . . . . . . . . . . . . . 231 5-13. Extending the Date Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 ■ CHAPTER 6 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Manipulating Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 6-1. Testing for Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 6-2. Counting the Occurrences of a Substring . . . . . . . . . . . . . . . . . . . . . . 269 6-3. Accessing Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 6-4. Using Substring Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 6-5. Replacing Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Processing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 6-6. Joining and Disassembling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 6-7. Reversing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 6-8. Controlling Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 6-9. Trimming Blank Spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 ■ CONTENTSviii 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page viii 6-10. Wrapping Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 6-11. Checking String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 6-12. Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 6-13. Comparing Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Project: Creating and Using a String Class . . . . . . . . . . . . . . . . . . . . . . . . . 285 6-14. Using a Page Reader Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 ■ CHAPTER 7 Working with Files and Directories . . . . . . . . . . . . . . . . . . . . . . . 291 Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 7-1. Opening Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 7-2. Reading from Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 7-3. Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 7-4. Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 7-5. Reading and Writing Comma-Separated Data . . . . . . . . . . . . . . . . . . 298 7-6. Reading Fixed-Width Delimited Data . . . . . . . . . . . . . . . . . . . . . . . . . . 300 7-7. Reading and Writing Binary Data in a File . . . . . . . . . . . . . . . . . . . . . 301 7-8. Getting the Number of Lines in a File . . . . . . . . . . . . . . . . . . . . . . . . . 303 7-9. Getting the Number of Characters, Words, or Paragraphs in a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 7-10. Project: Creating and Using a File Class . . . . . . . . . . . . . . . . . . . . . . 305 Working with Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 7-11. Listing All Files in the Current Directory . . . . . . . . . . . . . . . . . . . . . . 310 7-12. Listing All Files of a Certain Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 7-13. Sorting Files by Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 7-14. Generating a Recursive Directory Listing . . . . . . . . . . . . . . . . . . . . . 314 7-15. Using the SPL DirectoryIterator Object . . . . . . . . . . . . . . . . . . . . . . . 316 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 ■ CHAPTER 8 Working with Dynamic Imaging . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Working with Image Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 8-1. Working with JPGs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 8-2. Working with GIFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 8-3. Working with PNGs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Working with Image Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Creating an Image from Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 8-4. Creating a Blank Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 8-5. Creating and Using Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 ■ CONTENTS ix 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page ix [...]... programming languages Rather than forcing the developer to assign a given variable a data type and then assign a value to it (as in languages such as C++ and Java), PHP automatically assigns a data type to a variable when a value is allocated to it This makes PHP rather simple to use when declaring variables and inputting values into them PHP variables, of course, follow a certain set of rules All variables... rather powerful and verbose array processing capabilities Those familiar with a programming language such as C++ will feel right at home, as PHP 5 has a truly formidable set of array functionality Many types of arrays are available to you in PHP 5, and you have many different ways to work with them PHP 5 fully supports regular arrays, multidimensional arrays, and even the handy associative array Unlike the... string functions available to PHP the array functions are , actually rather well organized and follow fairly easy-to-use naming conventions that make it straightforward to work with them Setting up and assigning both associative arrays and regular arrays in PHP is easy PHP arrays start the index at zero, as most programming languages do Indexing arrays is just as easy; PHP 5 supports several methods to... while PHP variables can and will change data types on the fly, ways still exist to force a variable to retain a certain data type PHP supports both typecasting and methods that can force a variable into a certain data type Table 1-1 lists PHP 5 s data types Table 1-1 PHP 5 Data Types Data Type Description Boolean Stores either a true or false value Integer Stores a numeric value that is a whole number... Regular expressions are interesting animals Basically, a regular expression helps to validate against a certain pattern of characters Regular expressions provide you with a means to give your script an example, if you will, to compare its variables against By using PHP 5 s regular expressions, you can create something of a variable map that you can then compare a value against to determine its validity... something of an alias to a variable that will change the original value if you modify the alias This is quite different from value-assigned variables that are essentially copies of the original The following example shows a couple blocks of code to give you a good handle on PHP 5 variable functionality The Code < ?php //sample1_1 .php / /A properly set-up PHP variable $myvar = 0; //An improper PHP variable //$1myvar... Double Stores a numeric value that can contain a number of decimal places (commonly called a float) String Stores a chain of characters Array Stores an indexed container of values Object Stores an instance of a defined class Resource Holds a reference to an external source NULL Represents a variable that has no value Two all-inclusive functions in PHP both get and set the value of a variable Aptly titled... printf() and sprintf(), that allow you to output the display as you would like By using either of these functions, you can format your output data in several ways Consider the following example in which it is integral that the value be displayed as a dollar amount set to two decimal places: 50 92_Ch01_FINAL 8/26/ 05 9: 45 AM Page 5 1-3 ■ ARRAYS < ?php //Let's say you live in Canada and want to add GST tax to... < ?php //sample1 _5 .php $email = "lee@babinplanet.ca"; echo preg_match("/^( [a- zA-Z0-9])+([ .a- zA-Z0-9_-])*@( [a- zA-Z0-9_-])+➥ (. [a- zA-Z0-9_-]+)+ [a- zA-Z0-9_-]$/",$email); //Would return 1 (true) echo ""; $bademail = "leebabin.ca"; echo preg_match("/^( [a- zA-Z0-9])+([ .a- zA-Z0-9_-])*@( [a- zA-Z0-9_-])+➥ (. [a- zA-Z0-9_-]+)+ [a- zA-Z0-9_-]$/",$bademail); //Would return 0 (false) ?> 1 0 How It Works As you can... PHP 5 and its variable system, please see Chapter 10 by Frank M Kromann, where he will cover the wide world of variables in depth 3 50 92_Ch01_FINAL 4 8/26/ 05 9: 45 AM Page 4 1-2 ■ NUMBERS 1-2 Numbers As any good programming language should be able to, PHP is more than capable of taking care of any math problems you may have PHP 5 is especially flexible when dealing with numbers because its variable accessing . PHP 5 Recipes A Problem-Solution Approach Lee Babin, Nathan A. Good, Frank M. Kromann, Jon Stephens 50 92_Ch00_FM_FINAL 8/26/ 05 5:32 PM Page i PHP 5 Recipes: . Regular Expression Recipes: A Problem-Solution Approach (Apress, 20 05) , and Regular Expressions for Windows Developers: A Problem-Solution Approach (Apress,