SQL Web Database Applications Mysql Cookbook™ with PHP and Mysql PHP Cookbook™ Learning PHP 5 PHP Pocket Reference Upgrading to PHP 5
Total Page:16
File Type:pdf, Size:1020Kb
Download from Wow! eBook <www.wowebook.com> Programming PHP Programming Other resources from O’Reilly Related titles Managing and Using MySQL Web Database Applications MySQL Cookbook™ with PHP and MySQL PHP Cookbook™ Learning PHP 5 PHP Pocket Reference Upgrading to PHP 5 oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit con- ferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. SECOND EDITION Programming PHP Rasmus Lerdorf, Kevin Tatroe, and Peter MacIntyre Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Programming PHP, Second Edition by Rasmus Lerdorf, Kevin Tatroe, and Peter MacIntyre Copyright © 2006, 2002 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editors: Tatiana Apandi and Allison Randal Cover Designer: Ellie Volckhausen Production Editor: Matt Hutchinson Interior Designer: David Futato Production Services: GEX, Inc. llustrators: Robert Romano, Jessamyn Read, and Lesley Borash Printing History: March 2002: First Edition. April 2006: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming PHP, the image of a cuckoo, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN-10: 0-596-00681-0 ISBN-13: 978-0-596-00681-5 [M] [10/07] Table of Contents Foreword . ix Preface . xi 1. Introduction to PHP . 1 What Does PHP Do? 1 A Brief History of PHP 2 Installing PHP 7 A Walk Through PHP 9 2. Language Basics . 18 Lexical Structure 18 Data Types 25 Variables 32 Expressions and Operators 36 Flow-Control Statements 49 Including Code 57 Embedding PHP in Web Pages 59 3. Functions . 63 Calling a Function 63 Defining a Function 64 Variable Scope 66 Function Parameters 68 Return Values 71 Variable Functions 72 Anonymous Functions 73 v 4. Strings . 74 Quoting String Constants 74 Printing Strings 78 Accessing Individual Characters 82 Cleaning Strings 82 Encoding and Escaping 83 Comparing Strings 89 Manipulating and Searching Strings 91 Regular Expressions 97 POSIX-Style Regular Expressions 101 Perl-Compatible Regular Expressions 106 5. Arrays . 119 Indexed Versus Associative Arrays 119 Identifying Elements of an Array 120 Storing Data in Arrays 120 Multidimensional Arrays 123 Extracting Multiple Values 123 Converting Between Arrays and Variables 127 Traversing Arrays 128 Sorting 133 Acting on Entire Arrays 138 Using Arrays 140 6. Objects . 143 Terminology 144 Creating an Object 144 Accessing Properties and Methods 145 Declaring a Class 146 Introspection 153 Serialization 159 7. Web Techniques . 164 HTTP Basics 164 Variables 165 Server Information 166 Processing Forms 168 Setting Response Headers 182 Maintaining State 185 SSL 195 vi | Table of Contents 8. Databases . 196 Using PHP to Access a Database 196 Relational Databases and SQL 198 PEAR DB Basics 199 Advanced Database Techniques 205 Sample Application 211 9. Graphics . 225 Embedding an Image in a Page 225 The GD Extension 226 Basic Graphics Concepts 227 Creating and Drawing Images 228 Images with Text 232 Dynamically Generated Buttons 236 Scaling Images 239 Color Handling 240 10. PDF . 246 PDF Extensions 246 Documents and Pages 246 Text 248 11. XML . 261 Lightning Guide to XML 261 Generating XML 263 Parsing XML 264 Parsing XML with DOM 277 Parsing XML with SimpleXML 277 Transforming XML with XSLT 278 Web Services 280 12. Security . 285 Filter Input 285 Escape Output 290 Cross-Site Scripting 293 Session Fixation 294 File Uploads 295 File Access 296 PHP Code 299 Table of Contents | vii Shell Commands 300 More Information 301 Security Recap 301 13. Application Techniques . 302 Code Libraries 302 Templating Systems 303 Handling Output 306 Error Handling 308 Performance Tuning 313 14. Extending PHP . 321 Architectural Overview 321 What You’ll Need 322 Building Your First Extensions 323 The config.m4 File 333 Memory Management 336 The pval/zval Data Type 338 Parameter Handling 342 Returning Values 345 References 349 Global Variables 350 Creating Variables 353 Extension INI Entries 354 Resources 356 Where to Go from Here 358 15. PHP on Windows . 359 Installing and Configuring PHP on Windows 359 Writing Portable Code for Windows and Unix 363 Interfacing with COM 366 Interacting with ODBC Data Sources 372 A. Function Reference . 377 B. Extension Overview . 491 Index . 503 viii | Table of Contents Foreword Today, PHP is the most widely used programming language on the Web, with over 40 percent of all web applications written in PHP. It is installed on over 22 million domain servers (source: Netcraft), as shown in Figure F-1. The number of develop- ers using PHP has now reached over 2.5 million. The community developing PHP is very impressive, withover 450 CVS committers whowrote a total of 1 million lines of code. The PHP community is one of the most dynamic, with thousands of people sharing code, evangelizing about PHP, supporting each other, and creating many projects suchas Wikipedia, Mambo, PHP-Nuke, FUDforum, SugarCRM, and Horde, just to mention a few. IBM, Oracle Endorse PHP PHP 5.0 Yahoo! • XML, SOAP, OOP 20M Standardizes • Zend Studio on PHP PHP 4 15M Released • 1M Internet domains • Zend Engine • Zend Founded Zeek Suraski, Andi Gutmans 10M Rasmus develop Lerdolf PHP 3 develops PHP/Fl 5M PHP internet domains 1995 1997 1999 2000 2002 2004 2005 Figure F-1. PHP development timeline with usage Its simplicity is what made PHP so successful. Simplicity equals less code. Develop- ers and companies have been developing projects with PHP in a fraction of the time it would take another language. ix This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. Download from Wow! eBook <www.wowebook.com> Objects andPDO. issues suchas extending and securing PHP,are and getting discusses quality newer information. features It ofnizable covers XML all names and of in the the importantThis PHP PHP topics, community book plus have is unique contributed a to must-have it. forrenowned expertiseinupdatingthe“Security”chapter. So anybody you working know with thatthis PHP. you Some book. of Wez the modernizedyears most experience the recog- in “Extending PHP. PHP”revision Wez project chapter, Furlong is and and Peter Chrisand Chris MacIntyre, Kevin brought Shiflett a Tatroe his have Zend providedRasmus also Certified the Lerdorf, contributed guidelines Engineer the for to withmore initial this creator thanfive book. of The the first newest version author of on PHP the (then calledSupport fortheEclipseDevelopmentplatform PHP/FI), Zend PHPFramework ment anddeploymentenvironment.TheProject’sfirsttwoopeninitiativesare: level by creating an industrial-grade,munity de members facto around standard new PHP open webtion source application Project initiatives develop- initiated aimed by at Zend takingMySQL, Technologies PHP rallies Intel, to many the and, leading next most companies recently,The and Red future com- Hat of have PHP all looks endorsed very it. bright. The Leading new platform Collabora- vendors such as IBM, Oracle, x | clean, crisp,clear,correctwaystowritecode.ItisthewarbetweenPHPandC++/Java. who want simple,It sloppy, is flexible, a war humanThere between ways is the an to humans unacknowledged and write war the that code computer goes and scientists. on It every those is day who a in want the war world between of those programming. in collaborationwithpartners,aPHPIDEbasedontheEclipseplatform. Zend is joining the Eclipse Foundation as a Strategic Developer.opment anddeploymentofmission-criticalPHPwebapplications. It will develop, This is a web application framework that will accelerate and improve the devel- Foreword Copyright ©2007O’Reilly&Associates, Inc.Allrightsreserved. This istheTitleofBook,eMatter Edition Zend Technologies,Inc.,thePHPCompany Vice President,Marketing —Adam Bosworth —Michel Gerin Google Preface1 Now more than ever, the Web is a major vehicle for corporate and personal commu- nications. Web sites carry satellite images of Earthin its entirety, searchfor life in outer space, and house personal photo albums, business shopping carts, and prod- uct lists. Many of those web sites are driven by PHP, an open source scripting lan- guage primarily designed for generating HTML content. Since its inception in 1994, PHP has swept the Web and continues its phenomenal growthwithrecent endorsements by IBM and ORACLE corporations (to name a few).