PHP 5 Recipes: a Problem-Solution Approach Copyright © 2005 by Lee Babin, Nathan A
Total Page:16
File Type:pdf, Size:1020Kb
5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page i 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 ii 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 [email protected], 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 [email protected], 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 iii Contents at a Glance 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 iii 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page iv 5092_Ch00_FM_FINAL 8/26/05 5:32 PM Page v 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 vi vi ■CONTENTS 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 . ..