THE EXPERT’S VOICE® IN WEB DEVELOPMENT PHP and MySQL Recipes A Problem-Solution Approach — Second Edition — Frank M. Kromann www.it-ebooks.info PHP and MySQL Recipes A Problem-Solution Approach Second Edition Frank M. Kromann www.it-ebooks.info PHP and MySQL Recipes: A Problem-Solution Approach, Second Edition Frank M. Kromann Trabuco Canyon California, USA ISBN-13 (pbk): 978-1-4842-0606-5 ISBN-13 (electronic): 978-1-4842-0605-8 DOI 10.1007/978-1-4842-0605-8 Library of Congress Control Number: 2016943555 Copyright © 2016 by Frank M. Kromann This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Steve Anglin Technical Reviewer: Massimo Nardone Editorial Board: Steve Anglin, Pramila Balan, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing Coordinating Editor: Mark Powers Copy Editor: Karen Jameson Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected] , or visit w ww.springeronline.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected] , or visit w ww.apress.com . Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at w ww.apress.com/bulk-sales . Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com/9781484206065 . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper www.it-ebooks.info Contents at a Glance About the Author ..................................................................................................xxix About the Technical Reviewer ..............................................................................xxxi Introduction ........................................................................................................xxxiii ■ Chapter 1: Installation and Confi guration .............................................................1 ■ Chapter 2: Classes and Objects ...........................................................................29 ■ Chapter 3: Performing Math Operations ..............................................................61 ■ Chapter 4: Working with Arrays ..........................................................................77 ■ Chapter 5: Dates and Times ...............................................................................109 ■ Chapter 6: Strings..............................................................................................125 ■ Chapter 7: Files and Directories ........................................................................143 ■ Chapter 8: Dynamic Imaging .............................................................................169 ■ Chapter 9: Regular Expressions ........................................................................197 ■ Chapter 10: Variables ........................................................................................209 ■ Chapter 11: Functions .......................................................................................231 ■ Chapter 12: Web Fundamentals .........................................................................249 ■ Chapter 13: Creating and Using Forms ..............................................................271 ■ Chapter 14: XML, RSS, WDDX, and SOAP ...........................................................293 ■ Chapter 15: Data Exchange with JSON ..............................................................317 ■ Chapter 16: Using MySQL Databases .................................................................333 Index .....................................................................................................................357 iii www.it-ebooks.info www.it-ebooks.info Contents About the Author ..................................................................................................xxix About the Technical Reviewer ..............................................................................xxxi Introduction ........................................................................................................xxxiii ■ Chapter 1: Installation and Confi guration .............................................................1 Recipe 1-1. Installing PHP ................................................................................................1 Problem ..................................................................................................................................................1 Solution...................................................................................................................................................1 How it Works ...........................................................................................................................................2 Recipe 1-2. Confi guring PHP ..........................................................................................17 Problem ................................................................................................................................................17 Solution.................................................................................................................................................17 How it Works .........................................................................................................................................18 Recipe 1-3. Compiling PHP ............................................................................................21 Problem ................................................................................................................................................21 Solution.................................................................................................................................................21 How it Works .........................................................................................................................................22 Recipe 1-4. Installing MySQL .........................................................................................26 Problem ................................................................................................................................................26 Solution.................................................................................................................................................26 How it Works .........................................................................................................................................27 Recipe 1-5. Virtual Machines .........................................................................................27 Problem ................................................................................................................................................27 Solution.................................................................................................................................................27 How it Works .........................................................................................................................................28 v www.it-ebooks.info ■ CONTENTS ■ Chapter 2: Classes and Objects ...........................................................................29 Recipe 2-1. Writing a Simple Class ................................................................................29 Problem ................................................................................................................................................29 Solution.................................................................................................................................................29 How it Works .........................................................................................................................................29 Recipe 2-2. Writing a Base Class ...................................................................................30 Problem ................................................................................................................................................30 Solution.................................................................................................................................................30 How it Works .........................................................................................................................................30 Recipe 2-3. Writing an Abstract Class ............................................................................31 Problem ................................................................................................................................................31 Solution.................................................................................................................................................31 How it Works .........................................................................................................................................31 Recipe 2-4. Writing an Interface.....................................................................................33 Problem ................................................................................................................................................33 Solution.................................................................................................................................................33 How it Works .........................................................................................................................................33 Recipe 2-5. Class Members as Regular Functions .........................................................35 Problem ................................................................................................................................................35 Solution.................................................................................................................................................35 How it Works .........................................................................................................................................35 Recipe 2-6. Protecting Data and Methods .....................................................................36 Problem ................................................................................................................................................36 Solution.................................................................................................................................................36 How it Works .........................................................................................................................................36 Recipe 2-7. Mixed Static and Object Context .................................................................37 Problem ................................................................................................................................................37 Solution.................................................................................................................................................37 How it Works .........................................................................................................................................38 vi www.it-ebooks.info ■ CONTENTS Recipe 2-8. Referencing Class Members .......................................................................38 Problem ................................................................................................................................................38 Solution.................................................................................................................................................38 How it Works .........................................................................................................................................38 Recipe 2-9. Instantiation of Classes ...............................................................................39 Problem ................................................................................................................................................39 Solution.................................................................................................................................................39 How it Works .........................................................................................................................................40 Recipe 2-10. Printing Objects.........................................................................................41 Problem ................................................................................................................................................41 Solution.................................................................................................................................................41 How it Works .........................................................................................................................................41 Recipe 2-11. Variable Overloading .................................................................................42 Problem ................................................................................................................................................42 Solution.................................................................................................................................................42 How it Works .........................................................................................................................................42 Recipe 2-12. Serializing Types and Object .....................................................................44 Problem ................................................................................................................................................44 Solution.................................................................................................................................................44 How it Works .........................................................................................................................................45 Recipe 2-13. Copying an Object .....................................................................................46 Problem ................................................................................................................................................46 Solution.................................................................................................................................................46 How it Works .........................................................................................................................................46 Recipe 2-14. Using Objects as Functions .......................................................................47 Problem ................................................................................................................................................47 Solution.................................................................................................................................................47 How it Works .........................................................................................................................................47 vii www.it-ebooks.info ■ CONTENTS Recipe 2-15. Overloading Methods ................................................................................48 Problem ................................................................................................................................................48 Solution.................................................................................................................................................48 How it Works .........................................................................................................................................48 Recipe 2-16. Debugging Objects ....................................................................................49 Problem ................................................................................................................................................49 Solution.................................................................................................................................................49 How it Works .........................................................................................................................................50 Recipe 2-17. Using Objects without a Class ..................................................................51 Problem ................................................................................................................................................51 Solution.................................................................................................................................................51 How it Works .........................................................................................................................................51 Recipe 2-18. Directory Iteration .....................................................................................52 Problem ................................................................................................................................................52 Solution.................................................................................................................................................52 How it Works .........................................................................................................................................52 Recipe 2-19. Writing Reusable Code ..............................................................................53 Problem ................................................................................................................................................53 Solution.................................................................................................................................................53 How it Works .........................................................................................................................................53 Recipe 2-20. Avoiding Name Collisions ..........................................................................57 Problem ................................................................................................................................................57 Solution.................................................................................................................................................57 How it Works .........................................................................................................................................57 Recipe 2-21. Autoloading Classes on First Use ..............................................................59 Problem ................................................................................................................................................59 Solution.................................................................................................................................................59 How it Works .........................................................................................................................................59 viii www.it-ebooks.info ■ CONTENTS ■ Chapter 3: Performing Math Operations ..............................................................61 Recipe 3-1. Changing the Base Values for Numbers ......................................................62 Problem ................................................................................................................................................62 Solution.................................................................................................................................................62 How it Works .........................................................................................................................................62 Recipe 3-2. Converting to a Different Base Value ..........................................................63 Problem ................................................................................................................................................63 Solution.................................................................................................................................................63 How it Works .........................................................................................................................................63 Recipe 3-3. Storing Binary Values in Integers ................................................................64 Problem ................................................................................................................................................64 Solution.................................................................................................................................................64 How it Works .........................................................................................................................................64 Recipe 3-4. Setting and Clearing Bits in Binary .............................................................65 Problem ................................................................................................................................................65 Solution.................................................................................................................................................65 How it Works .........................................................................................................................................65 Recipe 3-5. Using Hexadecimal Numbers ......................................................................66 Problem ................................................................................................................................................66 Solution.................................................................................................................................................66 How it Works .........................................................................................................................................66 Recipe 3-6. Increasing Performance with Binary Shift ..................................................67 Problem ................................................................................................................................................67 Solution.................................................................................................................................................67 How it Works .........................................................................................................................................67 Recipe 3-7. Rounding Floating Point Numbers ..............................................................69 Problem ................................................................................................................................................69 Solution.................................................................................................................................................69 How it Works .........................................................................................................................................69 ix www.it-ebooks.info
Description: