Programming PHP, 2nd Edition

Book description

Programming PHP, 2nd Edition, is the authoritative guide to PHP 5 and is filled with the unique knowledge of the creator of PHP (Rasmus Lerdorf) and other PHP experts. When it comes to creating websites, the PHP scripting language is truly a red-hot property. In fact, PHP is currently used on more than 19 million websites, surpassing Microsoft's ASP .NET technology in popularity. Programmers love its flexibility and speed; designers love its accessibility and convenience.

As the industry standard book on PHP, all of the essentials are covered in a clear and concise manner. Language syntax and programming techniques are coupled with numerous examples that illustrate both correct usage and common idioms. With style tips and practical programming advice, this book will help you become not just a PHP programmer, but a good PHP programmer. Programming PHP, Second Edition covers everything you need to know to create effective web applications with PHP. Contents include:

  • Detailed information on the basics of the PHP language, including data types, variables, operators, and flow control statements
  • Chapters outlining the basics of functions, strings, arrays, and objects
  • Coverage of common PHP web application techniques, such as form processing and validation, session tracking, and cookies
  • Material on interacting with relational databases, such as MySQL and Oracle, using the database-independent PEAR DB library and the new PDO Library
  • Chapters that show you how to generate dynamic images, create PDF files, and parse XML files with PHP
  • Advanced topics, such as creating secure scripts, error handling, performance tuning, and writing your own C language extensions to PHP
  • A handy quick reference to all the core functions in PHP and all the standard extensions that ship with PHP

Praise for the first edition:

"If you are just getting into the dynamic Web development world or youare considering migrating from another dynamic web product to PHP, Programming PHP is the book of choice to get you up, running, and productive in a short time."

--Peter MacIntrye, eWeek

"I think this is a great book for programmers who want to start developing dynamic websites with PHP. It gives a detailed overview of PHP, lots of valuable tips, and a good sense of PHP's strengths."

--David Dooling, Slashdot.org

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Foreword
  3. Preface
    1. Audience
    2. Assumptions This Book Makes
    3. Contents of This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Comments and Questions
    7. Safari® Enabled
    8. Acknowledgments
      1. Rasmus Lerdorf
      2. Kevin Tatroe
      3. Peter MacIntyre
        1. Dedication
  4. 1. Introduction to PHP
    1. 1.1. What Does PHP Do?
    2. 1.2. A Brief History of PHP
      1. 1.2.1. The Evolution of PHP
      2. 1.2.2. The Growth of PHP
    3. 1.3. Installing PHP
    4. 1.4. A Walk Through PHP
      1. 1.4.1. Configuration Page
      2. 1.4.2. Forms
      3. 1.4.3. Databases
      4. 1.4.4. Graphics
      5. 1.4.5. From the Shell
  5. 2. Language Basics
    1. 2.1. Lexical Structure
      1. 2.1.1. Case Sensitivity
      2. 2.1.2. Statements and Semicolons
      3. 2.1.3. Whitespace and Line Breaks
      4. 2.1.4. Comments
        1. 2.1.4.1. Shell-style comments
        2. 2.1.4.2. C++ comments
        3. 2.1.4.3. C comments
      5. 2.1.5. Literals
      6. 2.1.6. Identifiers
        1. 2.1.6.1. Variable names
        2. 2.1.6.2. Function names
        3. 2.1.6.3. Class names
        4. 2.1.6.4. Constants
      7. 2.1.7. Keywords
    2. 2.2. Data Types
      1. 2.2.1. Integers
      2. 2.2.2. Floating-Point Numbers
      3. 2.2.3. Strings
      4. 2.2.4. Booleans
      5. 2.2.5. Arrays
      6. 2.2.6. Objects
      7. 2.2.7. Resources
      8. 2.2.8. NULL
    3. 2.3. Variables
      1. 2.3.1. Variable Variables
      2. 2.3.2. Variable References
      3. 2.3.3. Variable Scope
        1. 2.3.3.1. Local scope
        2. 2.3.3.2. Global scope
        3. 2.3.3.3. Static variables
        4. 2.3.3.4. Function parameters
      4. 2.3.4. Garbage Collection
    4. 2.4. Expressions and Operators
      1. 2.4.1. Number of Operands
      2. 2.4.2. Operator Precedence
      3. 2.4.3. Operator Associativity
      4. 2.4.4. Implicit Casting
      5. 2.4.5. Arithmetic Operators
      6. 2.4.6. String Concatenation Operator
      7. 2.4.7. Autoincrement and Autodecrement Operators
      8. 2.4.8. Comparison Operators
      9. 2.4.9. Bitwise Operators
      10. 2.4.10. Logical Operators
      11. 2.4.11. Casting Operators
      12. 2.4.12. Assignment Operators
        1. 2.4.12.1. Assignment
        2. 2.4.12.2. Assignment with operation
      13. 2.4.13. Miscellaneous Operators
    5. 2.5. Flow-Control Statements
      1. 2.5.1. if
      2. 2.5.2. switch
      3. 2.5.3. while
      4. 2.5.4. for
      5. 2.5.5. foreach
      6. 2.5.6. declare
      7. 2.5.7. exit and return
    6. 2.6. Including Code
    7. 2.7. Embedding PHP in Web Pages
      1. 2.7.1. XML Style
      2. 2.7.2. SGML Style
      3. 2.7.3. ASP Style
      4. 2.7.4. Script Style
      5. 2.7.5. Echoing Content Directly
  6. 3. Functions
    1. 3.1. Calling a Function
    2. 3.2. Defining a Function
    3. 3.3. Variable Scope
      1. 3.3.1. Global Variables
      2. 3.3.2. Static Variables
    4. 3.4. Function Parameters
      1. 3.4.1. Passing Parameters by Value
      2. 3.4.2. Passing Parameters by Reference
      3. 3.4.3. Default Parameters
      4. 3.4.4. Variable Parameters
      5. 3.4.5. Missing Parameters
    5. 3.5. Return Values
    6. 3.6. Variable Functions
    7. 3.7. Anonymous Functions
  7. 4. Strings
    1. 4.1. Quoting String Constants
      1. 4.1.1. Variable Interpolation
      2. 4.1.2. Single-Quoted Strings
      3. 4.1.3. Double-Quoted Strings
      4. 4.1.4. Here Documents
    2. 4.2. Printing Strings
      1. 4.2.1. echo
      2. 4.2.2. print( )
      3. 4.2.3. printf( )
        1. 4.2.3.1. Format modifiers
        2. 4.2.3.2. Type specifiers
      4. 4.2.4. print_r( ) and var_dump( )
    3. 4.3. Accessing Individual Characters
    4. 4.4. Cleaning Strings
      1. 4.4.1. Removing Whitespace
      2. 4.4.2. Changing Case
    5. 4.5. Encoding and Escaping
      1. 4.5.1. HTML
        1. 4.5.1.1. Entity-quoting all special characters
        2. 4.5.1.2. Entity-quoting only HTML syntax characters
        3. 4.5.1.3. Removing HTML tags
        4. 4.5.1.4. Extracting meta tags
      2. 4.5.2. URLs
        1. 4.5.2.1. RFC 1738 encoding and decoding
        2. 4.5.2.2. Query-string encoding
      3. 4.5.3. SQL
      4. 4.5.4. C-String Encoding
    6. 4.6. Comparing Strings
      1. 4.6.1. Exact Comparisons
      2. 4.6.2. Approximate Equality
    7. 4.7. Manipulating and Searching Strings
      1. 4.7.1. Substrings
      2. 4.7.2. Miscellaneous String Functions
      3. 4.7.3. Decomposing a String
        1. 4.7.3.1. Exploding and imploding
        2. 4.7.3.2. Tokenizing
        3. 4.7.3.3. sscanf( )
      4. 4.7.4. String-Searching Functions
        1. 4.7.4.1. Searches returning position
        2. 4.7.4.2. Searches returning rest of string
        3. 4.7.4.3. Searches using masks
        4. 4.7.4.4. Decomposing URLs
    8. 4.8. Regular Expressions
      1. 4.8.1. The Basics
      2. 4.8.2. Character Classes
      3. 4.8.3. Alternatives
      4. 4.8.4. Repeating Sequences
      5. 4.8.5. Subpatterns
    9. 4.9. POSIX-Style Regular Expressions
      1. 4.9.1. Character Classes
      2. 4.9.2. Anchors
      3. 4.9.3. Functions
        1. 4.9.3.1. Matching
        2. 4.9.3.2. Replacing
        3. 4.9.3.3. Splitting
    10. 4.10. Perl-Compatible Regular Expressions
      1. 4.10.1. Delimiters
      2. 4.10.2. Match Behavior
      3. 4.10.3. Character Classes
      4. 4.10.4. Anchors
      5. 4.10.5. Quantifiers and Greed
      6. 4.10.6. Non-Capturing Groups
      7. 4.10.7. Backreferences
      8. 4.10.8. Trailing Options
      9. 4.10.9. Inline Options
      10. 4.10.10. Lookahead and Lookbehind
      11. 4.10.11. Cut
      12. 4.10.12. Conditional Expressions
      13. 4.10.13. Functions
        1. 4.10.13.1. Matching
        2. 4.10.13.2. Replacing
        3. 4.10.13.3. Splitting
        4. 4.10.13.4. Filtering an array with a regular expression
        5. 4.10.13.5. Quoting for regular expressions
      14. 4.10.14. Differences from Perl Regular Expressions
  8. 5. Arrays
    1. 5.1. Indexed Versus Associative Arrays
    2. 5.2. Identifying Elements of an Array
    3. 5.3. Storing Data in Arrays
      1. 5.3.1. Adding Values to the End of an Array
      2. 5.3.2. Assigning a Range of Values
      3. 5.3.3. Getting the Size of an Array
      4. 5.3.4. Padding an Array
    4. 5.4. Multidimensional Arrays
    5. 5.5. Extracting Multiple Values
      1. 5.5.1. Slicing an Array
      2. 5.5.2. Splitting an Array into Chunks
      3. 5.5.3. Keys and Values
      4. 5.5.4. Checking Whether an Element Exists
      5. 5.5.5. Removing and Inserting Elements in an Array
    6. 5.6. Converting Between Arrays and Variables
      1. 5.6.1. Creating Variables from an Array
      2. 5.6.2. Creating an Array from Variables
    7. 5.7. Traversing Arrays
      1. 5.7.1. The foreach Construct
      2. 5.7.2. The Iterator Functions
      3. 5.7.3. Using a for Loop
      4. 5.7.4. Calling a Function for Each Array Element
      5. 5.7.5. Reducing an Array
      6. 5.7.6. Searching for Values
    8. 5.8. Sorting
      1. 5.8.1. Sorting One Array at a Time
      2. 5.8.2. Natural-Order Sorting
      3. 5.8.3. Sorting Multiple Arrays at Once
      4. 5.8.4. Reversing Arrays
      5. 5.8.5. Randomizing Order
    9. 5.9. Acting on Entire Arrays
      1. 5.9.1. Calculating the Sum of an Array
      2. 5.9.2. Merging Two Arrays
      3. 5.9.3. Calculating the Difference Between Two Arrays
      4. 5.9.4. Filtering Elements from an Array
    10. 5.10. Using Arrays
      1. 5.10.1. Sets
      2. 5.10.2. Stacks
  9. 6. Objects
    1. 6.1. Terminology
    2. 6.2. Creating an Object
    3. 6.3. Accessing Properties and Methods
    4. 6.4. Declaring a Class
      1. 6.4.1. Declaring Methods
      2. 6.4.2. Declaring Properties
      3. 6.4.3. Declaring Constants
      4. 6.4.4. Inheritance
      5. 6.4.5. Interfaces
      6. 6.4.6. Abstract Methods
      7. 6.4.7. Constructors
      8. 6.4.8. Destructors
    5. 6.5. Introspection
      1. 6.5.1. Examining Classes
      2. 6.5.2. Examining an Object
      3. 6.5.3. Sample Introspection Program
    6. 6.6. Serialization
  10. 7. Web Techniques
    1. 7.1. HTTP Basics
    2. 7.2. Variables
    3. 7.3. Server Information
    4. 7.4. Processing Forms
      1. 7.4.1. Methods
      2. 7.4.2. Parameters
      3. 7.4.3. Automatic Quoting of Parameters
      4. 7.4.4. Self-Processing Pages
      5. 7.4.5. Sticky Forms
      6. 7.4.6. Multivalued Parameters
      7. 7.4.7. Sticky Multivalued Parameters
      8. 7.4.8. File Uploads
      9. 7.4.9. Form Validation
    5. 7.5. Setting Response Headers
      1. 7.5.1. Different Content Types
      2. 7.5.2. Redirections
      3. 7.5.3. Expiration
      4. 7.5.4. Authentication
    6. 7.6. Maintaining State
      1. 7.6.1. Cookies
      2. 7.6.2. Sessions
        1. 7.6.2.1. Session basics
        2. 7.6.2.2. Alternatives to cookies
        3. 7.6.2.3. Custom storage
      3. 7.6.3. Combining Cookies and Sessions
    7. 7.7. SSL
  11. 8. Databases
    1. 8.1. Using PHP to Access a Database
    2. 8.2. Relational Databases and SQL
    3. 8.3. PEAR DB Basics
      1. 8.3.1. Data Source Names
      2. 8.3.2. Connecting
      3. 8.3.3. Error Checking
      4. 8.3.4. Issuing a Query
      5. 8.3.5. Fetching Results from a Query
        1. 8.3.5.1. Returning the row
        2. 8.3.5.2. Storing the row
        3. 8.3.5.3. Inside a row array
        4. 8.3.5.4. Finishing the result
      6. 8.3.6. Disconnecting
    4. 8.4. Advanced Database Techniques
      1. 8.4.1. Placeholders
      2. 8.4.2. Prepare/Execute
      3. 8.4.3. Shortcuts
      4. 8.4.4. Details About a Query Response
      5. 8.4.5. Sequences
      6. 8.4.6. Metadata
      7. 8.4.7. Transactions
    5. 8.5. Sample Application
      1. 8.5.1. Database Tables
      2. 8.5.2. Database Connection
      3. 8.5.3. Administrator’s Page
      4. 8.5.4. Adding a Business
      5. 8.5.5. Displaying the Database
      6. 8.5.6. PHP Data Objects
        1. 8.5.6.1. Making a connection
        2. 8.5.6.2. Interaction with the database
        3. 8.5.6.3. PDO and prepared statements
  12. 9. Graphics
    1. 9.1. Embedding an Image in a Page
    2. 9.2. The GD Extension
    3. 9.3. Basic Graphics Concepts
    4. 9.4. Creating and Drawing Images
      1. 9.4.1. The Structure of a Graphics Program
      2. 9.4.2. Changing the Output Format
      3. 9.4.3. Testing for Supported Image Formats
      4. 9.4.4. Reading an Existing File
      5. 9.4.5. Basic Drawing Functions
    5. 9.5. Images with Text
      1. 9.5.1. Fonts
      2. 9.5.2. TrueType Fonts
    6. 9.6. Dynamically Generated Buttons
      1. 9.6.1. Caching the Dynamically Generated Buttons
      2. 9.6.2. A Faster Cache
    7. 9.7. Scaling Images
    8. 9.8. Color Handling
      1. 9.8.1. Using the Alpha Channel
      2. 9.8.2. Identifying Colors
      3. 9.8.3. True Color Color Indexes
      4. 9.8.4. Text Representation of an Image
  13. 10. PDF
    1. 10.1. PDF Extensions
    2. 10.2. Documents and Pages
      1. 10.2.1. A Simple Example
      2. 10.2.2. Initializing the Document
      3. 10.2.3. Outputting Basic Text - Cells
    3. 10.3. Text
      1. 10.3.1. Coordinates
      2. 10.3.2. Text Attributes
      3. 10.3.3. Page Headers, Footers, and Class Extension
      4. 10.3.4. Images and Links
      5. 10.3.5. Tables and Data
  14. 11. XML
    1. 11.1. Lightning Guide to XML
    2. 11.2. Generating XML
    3. 11.3. Parsing XML
      1. 11.3.1. Element Handlers
      2. 11.3.2. Character Data Handler
      3. 11.3.3. Processing Instructions
      4. 11.3.4. Entity Handlers
        1. 11.3.4.1. External entities
        2. 11.3.4.2. Unparsed entities
      5. 11.3.5. Default Handler
      6. 11.3.6. Options
        1. 11.3.6.1. Character encoding
        2. 11.3.6.2. Case folding
      7. 11.3.7. Using the Parser
      8. 11.3.8. Errors
      9. 11.3.9. Methods as Handlers
      10. 11.3.10. Sample Parsing Application
    4. 11.4. Parsing XML with DOM
    5. 11.5. Parsing XML with SimpleXML
    6. 11.6. Transforming XML with XSLT
    7. 11.7. Web Services
      1. 11.7.1. Servers
      2. 11.7.2. Clients
  15. 12. Security
    1. 12.1. Filter Input
      1. 12.1.1. SQL Injection
    2. 12.2. Escape Output
      1. 12.2.1. Filenames
        1. 12.2.1.1. Check for relative paths
    3. 12.3. Cross-Site Scripting
    4. 12.4. Session Fixation
    5. 12.5. File Uploads
      1. 12.5.1. Distrust Browser-Supplied Filenames
      2. 12.5.2. Beware of Filling Your Filesystem
      3. 12.5.3. Surviving register_globals
    6. 12.6. File Access
      1. 12.6.1. Restrict Filesystem Access to a Specific Directory
      2. 12.6.2. Get It Right the First Time
      3. 12.6.3. Don’t Use Files
      4. 12.6.4. Session Files
      5. 12.6.5. Concealing PHP Libraries
    7. 12.7. PHP Code
    8. 12.8. Shell Commands
    9. 12.9. More Information
    10. 12.10. Security Recap
  16. 13. Application Techniques
    1. 13.1. Code Libraries
    2. 13.2. Templating Systems
    3. 13.3. Handling Output
      1. 13.3.1. Output Buffering
      2. 13.3.2. Compressing Output
    4. 13.4. Error Handling
      1. 13.4.1. Error Reporting
      2. 13.4.2. Error Suppression
      3. 13.4.3. Triggering Errors
      4. 13.4.4. Defining Error Handlers
        1. 13.4.4.1. Logging in error handlers
        2. 13.4.4.2. Output buffering in error handlers
    5. 13.5. Performance Tuning
      1. 13.5.1. Benchmarking
      2. 13.5.2. Profiling
      3. 13.5.3. Optimizing Execution Time
      4. 13.5.4. Optimizing Memory Requirements
      5. 13.5.5. Reverse Proxies and Replication
        1. 13.5.5.1. Reverse-proxy cache
        2. 13.5.5.2. Load balancing and redirection
        3. 13.5.5.3. MySQL replication
        4. 13.5.5.4. Putting it all together
  17. 14. Extending PHP
    1. 14.1. Architectural Overview
    2. 14.2. What You’ll Need
      1. 14.2.1. The PHP Source
      2. 14.2.2. Software Tools
    3. 14.3. Building Your First Extensions
      1. 14.3.1. Command-Line PHP
      2. 14.3.2. Planning Your Extension
      3. 14.3.3. Creating a Skeleton Extension
      4. 14.3.4. Fleshing Out the Skeleton
      5. 14.3.5. Compiling Your Extension
        1. 14.3.5.1. Standalone extensions
        2. 14.3.5.2. Compiling the extension into PHP
      6. 14.3.6. Testing Your Extension
    4. 14.4. The config.m4 File
      1. 14.4.1. No External Dependencies
      2. 14.4.2. External Dependencies
    5. 14.5. Memory Management
    6. 14.6. The pval /zval Data Type
      1. 14.6.1. MAKE_STD_ZVAL( )
      2. 14.6.2. SEPARATE_ZVAL( )
      3. 14.6.3. zval_copy_ctor( )
      4. 14.6.4. Accessor Macros
    7. 14.7. Parameter Handling
      1. 14.7.1. A Simple Example
      2. 14.7.2. A More Complex Example
      3. 14.7.3. An Example with Variable Argument List
    8. 14.8. Returning Values
      1. 14.8.1. Simple Types
      2. 14.8.2. Arrays
      3. 14.8.3. Objects
    9. 14.9. References
    10. 14.10. Global Variables
      1. 14.10.1. SAPI Globals (SG)
      2. 14.10.2. Executor Globals (EG )
      3. 14.10.3. Internal Extension Globals
    11. 14.11. Creating Variables
    12. 14.12. Extension INI Entries
    13. 14.13. Resources
    14. 14.14. Where to Go from Here
  18. 15. PHP on Windows
    1. 15.1. Installing and Configuring PHP on Windows
      1. 15.1.1. Going Straight to the Source
      2. 15.1.2. Configuring PHP with a Web Server
        1. 15.1.2.1. Configuration common to all Microsoft installations
        2. 15.1.2.2. Manually configuring Apache
        3. 15.1.2.3. Other installers and prepackaged distributions
      3. 15.1.3. Adding Extensions to the Base Distribution
      4. 15.1.4. Adding the MySQL Extension
    2. 15.2. Writing Portable Code for Windows and Unix
      1. 15.2.1. Determining the Platform
      2. 15.2.2. Handling Paths Across Platforms
      3. 15.2.3. The Environment
      4. 15.2.4. Sending Mail
      5. 15.2.5. End-of-Line Handling
      6. 15.2.6. End-of-File Handling
      7. 15.2.7. External Commands
      8. 15.2.8. Common Platform-Specific Extensions
    3. 15.3. Interfacing with COM
      1. 15.3.1. Background
      2. 15.3.2. PHP Functions
      3. 15.3.3. Determining the API
      4. 15.3.4. Completing a Word Document
    4. 15.4. Interacting with ODBC Data Sources
      1. 15.4.1. Configuring a DSN
      2. 15.4.2. Working with Access
  19. A. Function Reference
    1. A.1. PHP Functions by Category
      1. A.1.1. Arrays
      2. A.1.2. Classes and Objects
      3. A.1.3. Date and Time
      4. A.1.4. Errors and Logging
      5. A.1.5. Files, Directories, and Filesystem
      6. A.1.6. Functions
      7. A.1.7. HTTP
      8. A.1.8. Images
      9. A.1.9. Mail
      10. A.1.10. Math
      11. A.1.11. Network
      12. A.1.12. Output Control
      13. A.1.13. PHP Options/Info
      14. A.1.14. Program Execution
      15. A.1.15. Strings
      16. A.1.16. Type Functions
      17. A.1.17. URLs
      18. A.1.18. Variable Functions
    2. A.2. Alphabetical Listing of PHP Functions
  20. B. Extension Overview
    1. B.1. Optional Extensions Listing
  21. About the Authors
  22. Colophon
  23. Copyright

Product information

  • Title: Programming PHP, 2nd Edition
  • Author(s): Rasmus Lerdorf, Kevin Tatroe, Peter MacIntyre
  • Release date: April 2006
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596006815