PHP6 and MySQL® 6 Bible

Book description

MySQL is the leading open source database on the market and PHP continues to dominate the server side of the scripting market—together, they are the most popular and common team for creating dynamic, database-driven web sites. This comprehensive book covers the newest version of PHP and MySQL and is packed with extensive code examples, full working applications, and valuable troubleshooting advice. You’ll explore installing, developing, and debugging the latest versions of PHP and MySQL and get insight on advanced topics such as error handling, debugging, PEAR, security, HTTP, cookies, and OOP.

Table of contents

  1. Copyright
  2. About the Authors
  3. Credits
  4. Acknowledgments
  5. Introduction
    1. What Is PHP?
    2. Why PHP?
    3. What's New in This Edition?
      1. New PHP 6 features
      2. Who wrote the book?
    4. Whom This Book Is For
    5. This Book Is Not the Manual
    6. How the Book Is Organized
      1. Part I: PHP: The Basics
      2. Part II: PHP and MySQL
      3. Part III: Advanced Techniques
      4. Part IV: Connections
      5. Part V: Case Studies
      6. Appendices
    7. Conventions Used in This Book
    8. What the Icons Mean
  6. I. Introducing PHP
    1. 1. Why PHP and MySQL?
      1. 1.1. What Is PHP?
      2. 1.2. What Is MySQL?
      3. 1.3. Deciding on a Web Application Platform
        1. 1.3.1. Cost
        2. 1.3.2. Ease of Use
        3. 1.3.3. HTML-embeddedness
        4. 1.3.4. Cross-platform compatibility
        5. 1.3.5. Stability
        6. 1.3.6. Many extensions
        7. 1.3.7. Fast feature development
        8. 1.3.8. Not proprietary
        9. 1.3.9. Strong user communities
      4. 1.4. Summary
    2. 2. Server-Side Scripting Overview
      1. 2.1. Static HTML
      2. 2.2. Client-Side Technologies
      3. 2.3. Server-Side Scripting
      4. 2.4. What Is Server-Side Scripting Good For?
      5. 2.5. Summary
    3. 3. Getting Started with PHP
      1. 3.1. Installing PHP
        1. 3.1.1. Installation procedures
          1. 3.1.1.1. Installing PHP on CentOS
          2. 3.1.1.2. Installing PHP on Debian
          3. 3.1.1.3. Installing PHP from source
          4. 3.1.1.4. Microsoft Windows and Apache
          5. 3.1.1.5. Other web servers
        2. 3.1.2. Development tools
      2. 3.2. What's to Come?
      3. 3.3. Your HTML Is Already PHP-Compliant!
      4. 3.4. Escaping from HTML
        1. 3.4.1. Canonical PHP tags
        2. 3.4.2. Hello World
        3. 3.4.3. Jumping in and out of PHP mode
        4. 3.4.4. Including files
      5. 3.5. Summary
    4. 4. Learning PHP Syntax and Variables
      1. 4.1. PHP Is Forgiving
      2. 4.2. HTML Is Not PHP
      3. 4.3. PHP's Syntax Is C-Like
        1. 4.3.1. PHP is whitespace insensitive
        2. 4.3.2. PHP is sometimes case sensitive
        3. 4.3.3. Statements are expressions terminated by semicolons
          1. 4.3.3.1. Expressions are combinations of tokens
          2. 4.3.3.2. Expressions are evaluated
          3. 4.3.3.3. Precedence, associativity, and evaluation order
          4. 4.3.3.4. Expressions and types
          5. 4.3.3.5. Assignment expressions
          6. 4.3.3.6. Reasons for expressions and statements
        4. 4.3.4. Braces make blocks
      4. 4.4. Comments
        1. 4.4.1. C-style multiline comments
        2. 4.4.2. Single-line comments: # and //
      5. 4.5. Variables
        1. 4.5.1. PHP variables are Perl-like
        2. 4.5.2. Declaring variables (or not)
        3. 4.5.3. Assigning variables
        4. 4.5.4. Reassigning variables
        5. 4.5.5. Unassigned variables
          1. 4.5.5.1. Default values
          2. 4.5.5.2. Checking assignment with isset
        6. 4.5.6. Variable scope
          1. 4.5.6.1. Functions and variable scope
        7. 4.5.7. You can switch modes if you want
        8. 4.5.8. Constants
      6. 4.6. Types in PHP: Don't Worry, Be Happy
        1. 4.6.1. No variable type declarations
        2. 4.6.2. Automatic type conversion
        3. 4.6.3. Types assigned by context
      7. 4.7. Type Summary
      8. 4.8. The Simple Types
        1. 4.8.1. Integers
          1. 4.8.1.1. Read formats
          2. 4.8.1.2. Range
        2. 4.8.2. Doubles
          1. 4.8.2.1. Read formats
        3. 4.8.3. Booleans
          1. 4.8.3.1. Boolean constants
          2. 4.8.3.2. Interpreting other types as Booleans
          3. 4.8.3.3. Examples
            1. 4.8.3.3.1. Don't use doubles as Booleans
        4. 4.8.4. NULL
        5. 4.8.5. Strings
          1. 4.8.5.1. Singly quoted strings
          2. 4.8.5.2. Doubly quoted strings
          3. 4.8.5.3. Single versus double quotation marks
          4. 4.8.5.4. Variable interpolation
          5. 4.8.5.5. Newlines in strings
          6. 4.8.5.6. Limits
      9. 4.9. Output
        1. 4.9.1. Echo and print
          1. 4.9.1.1. Echo
          2. 4.9.1.2. Print
        2. 4.9.2. Variables and strings
          1. 4.9.2.1. HTML and linebreaks
      10. 4.10. Summary
    5. 5. Learning PHP Control Structures and Functions
      1. 5.1. Boolean Expressions
        1. 5.1.1. Boolean constants
        2. 5.1.2. Logical operators
          1. 5.1.2.1. Precedence of logical operators
          2. 5.1.2.2. Logical operators short-circuit
        3. 5.1.3. Comparison operators
          1. 5.1.3.1. Operator precedence
          2. 5.1.3.2. String comparison
        4. 5.1.4. The ternary operator
      2. 5.2. Branching
        1. 5.2.1. If-else
          1. 5.2.1.1. Else attachment
          2. 5.2.1.2. Elseif
        2. 5.2.2. Switch
      3. 5.3. Looping
        1. 5.3.1. Bounded loops versus unbounded loops
        2. 5.3.2. While
        3. 5.3.3. Do-while
        4. 5.3.4. For
        5. 5.3.5. Looping examples
          1. 5.3.5.1. A bounded for loop
          2. 5.3.5.2. An unbounded while loop
        6. 5.3.6. Break and continue
        7. 5.3.7. A note on infinite loops
      4. 5.4. Alternate Control Syntaxes
      5. 5.5. Terminating Execution
      6. 5.6. Using Functions
        1. 5.6.1. Return values versus side effects
      7. 5.7. Function Documentation
        1. 5.7.1. Headers in documentation
        2. 5.7.2. Finding function documentation
      8. 5.8. Defining Your Own Functions
        1. 5.8.1. What is a function?
        2. 5.8.2. Function definition syntax
        3. 5.8.3. Function definition example
        4. 5.8.4. Formal parameters versus actual parameters
        5. 5.8.5. Argument number mismatches
          1. 5.8.5.1. Too few arguments
          2. 5.8.5.2. Too many arguments
      9. 5.9. Functions and Variable Scope
        1. 5.9.1. Global versus local
        2. 5.9.2. Static variables
        3. 5.9.3. Exceptions
      10. 5.10. Function Scope
        1. 5.10.1. Include and require
          1. 5.10.1.1. Including only once
          2. 5.10.1.2. The include path
        2. 5.10.2. Recursion
      11. 5.11. Summary
    6. 6. Passing Information with PHP
      1. 6.1. HTTP Is Stateless
      2. 6.2. GET Arguments
      3. 6.3. A Better Use for GET-Style URLs
      4. 6.4. POST Arguments
      5. 6.5. Formatting Form Variables
        1. 6.5.1. Consolidating forms and form handlers
      6. 6.6. PHP Superglobal Arrays
      7. 6.7. Summary
    7. 7. Learning PHP String Handling
      1. 7.1. Strings in PHP
        1. 7.1.1. Interpolation with curly braces
        2. 7.1.2. Characters and string indexes
        3. 7.1.3. String operators
        4. 7.1.4. Concatenation and assignment
        5. 7.1.5. The heredoc syntax
      2. 7.2. String Functions
        1. 7.2.1. Inspecting strings
        2. 7.2.2. Finding characters and substrings
        3. 7.2.3. Comparison and searching
        4. 7.2.4. Searching
        5. 7.2.5. Substring selection
        6. 7.2.6. String cleanup functions
        7. 7.2.7. String replacement
        8. 7.2.8. Case functions
          1. 7.2.8.1. strtolower()
          2. 7.2.8.2. strtoupper()
          3. 7.2.8.3. ucfirst()
          4. 7.2.8.4. ucwords()
        9. 7.2.9. Escaping functions
        10. 7.2.10. Printing and output
      3. 7.3. Summary
    8. 8. Learning Arrays
      1. 8.1. The Uses of Arrays
      2. 8.2. What Are PHP Arrays?
      3. 8.3. Creating Arrays
        1. 8.3.1. Direct assignment
        2. 8.3.2. The array() construct
        3. 8.3.3. Specifying indices using array()
        4. 8.3.4. Functions returning arrays
      4. 8.4. Retrieving Values
        1. 8.4.1. Retrieving by index
        2. 8.4.2. The list() construct
      5. 8.5. Multidimensional Arrays
      6. 8.6. Inspecting Arrays
      7. 8.7. Deleting from Arrays
      8. 8.8. Iteration
        1. 8.8.1. Support for iteration
        2. 8.8.2. Using iteration functions
        3. 8.8.3. Our favorite iteration method: foreach
        4. 8.8.4. Iterating with current() and next()
        5. 8.8.5. Starting over with reset()
        6. 8.8.6. Reverse order with end() and prev()
        7. 8.8.7. Extracting keys with key()
        8. 8.8.8. Empty values and the each() function
        9. 8.8.9. Walking with array_walk()
      9. 8.9. Summary
    9. 9. Learning PHP Number Handling
      1. 9.1. Numerical Types
      2. 9.2. Mathematical Operators
        1. 9.2.1. Arithmetic operators
        2. 9.2.2. Arithmetic operators and types
        3. 9.2.3. Incrementing operators
        4. 9.2.4. Assignment operators
        5. 9.2.5. Comparison operators
        6. 9.2.6. Precedence and parentheses
      3. 9.3. Simple Mathematical Functions
      4. 9.4. Randomness
        1. 9.4.1. Seeding the generator
        2. 9.4.2. Example: Making a random selection
      5. 9.5. Summary
    10. 10. PHP Gotchas
      1. 10.1. Installation-Related Problems
        1. 10.1.1. Symptom: Text of file displayed in browser window
        2. 10.1.2. Symptom: PHP blocks showing up as text under HTTP or browser prompts you to save file
        3. 10.1.3. Symptom: Server or host not found/Page cannot be displayed
      2. 10.2. Rendering Problems
        1. 10.2.1. Symptom: Totally blank page
        2. 10.2.2. Symptom: PHP code showing up in Web browser
      3. 10.3. Failures to Load Page
        1. 10.3.1. Symptom: Page cannot be found
        2. 10.3.2. Symptom: Failed opening [file] for inclusion
      4. 10.4. Parse Errors
        1. 10.4.1. Symptom: Parse error message
        2. 10.4.2. The missing semicolon
        3. 10.4.3. No dollar signs
        4. 10.4.4. Mode issues
        5. 10.4.5. Unescaped quotation marks
        6. 10.4.6. Unterminated strings
        7. 10.4.7. Other parse error causes
      5. 10.5. Missing Includes
        1. 10.5.1. Symptom: Include warning
      6. 10.6. Unbound Variables
        1. 10.6.1. Symptom: Variable not showing up in print string
        2. 10.6.2. Symptom: Numerical variable unexpectedly zero
        3. 10.6.3. Causes of unbound variables
          1. 10.6.3.1. Case problems
          2. 10.6.3.2. Scoping problems
      7. 10.7. Function Problems
        1. 10.7.1. Symptom: Call to undefined function my_function()
        2. 10.7.2. Symptom: Call to undefined function ()
        3. 10.7.3. Symptom: Call to undefined function array()
        4. 10.7.4. Symptom: Cannot redeclare my_function()
        5. 10.7.5. Symptom: Wrong parameter count
      8. 10.8. Math Problems
        1. 10.8.1. Symptom: Division-by-zero warning
        2. 10.8.2. Symptom: Unexpected arithmetic result
        3. 10.8.3. Symptom: NaN (or NAN)
      9. 10.9. Timeouts
      10. 10.10. Summary
  7. II. MySQL Database Integration
    1. 11. Introducing Databases and MySQL
      1. 11.1. What Is a Database?
      2. 11.2. Why a Database?
        1. 11.2.1. Maintainability and scalability
        2. 11.2.2. Portability
        3. 11.2.3. Avoiding awkward programming
        4. 11.2.4. Searching
      3. 11.3. PHP-Supported Databases
      4. 11.4. Our Focus: MySQL
      5. 11.5. Summary
    2. 12. Installing MySQL
      1. 12.1. Obtaining MySQL
      2. 12.2. Installing MySQL on Linux
        1. 12.2.1. Installing MySQL Server on Debian and Ubuntu
      3. 12.3. Installing MySQL on Microsoft Windows
        1. 12.3.1. Installing MySQL on Windows
      4. 12.4. Summary
    3. 13. Learning Structured Query Language (SQL)
      1. 13.1. Relational Databases and SQL
      2. 13.2. SQL Standards
      3. 13.3. The Workhorses of SQL
        1. 13.3.1. SELECT
          1. 13.3.1.1. Selecting Certain Records
          2. 13.3.1.2. Joins
          3. 13.3.1.3. Subselects
        2. 13.3.2. INSERT
        3. 13.3.3. UPDATE
        4. 13.3.4. DELETE
      4. 13.4. Database Design
      5. 13.5. Privileges and Security
        1. 13.5.1. Setting database permissions
        2. 13.5.2. Keep database passwords outside the web area
        3. 13.5.3. Learn to make backups
      6. 13.6. Summary
    4. 14. Learning Database Administration and Design
      1. 14.1. Basic MySQL Client Commands
      2. 14.2. MySQL User Administration
        1. 14.2.1. Local development
        2. 14.2.2. Standalone web site
        3. 14.2.3. Shared-hosting web site
      3. 14.3. Backups
      4. 14.4. Replication
      5. 14.5. Recovery
        1. 14.5.1. myisamchk
        2. 14.5.2. mysqlcheck
      6. 14.6. Summary
    5. 15. Integrating PHP and MySQL
      1. 15.1. Connecting to MySQL
      2. 15.2. Making MySQL Queries
      3. 15.3. Fetching Data Sets
      4. 15.4. Getting Data about Data
      5. 15.5. Multiple Connections
      6. 15.6. Building in Error Checking
      7. 15.7. Creating MySQL Databases with PHP
        1. 15.7.1. MySQL data types
      8. 15.8. MySQL Functions
      9. 15.9. Summary
    6. 16. Performing Database Queries
      1. 16.1. HTML Tables and Database Tables
        1. 16.1.1. One-to-one mapping
        2. 16.1.2. Example: A single-table displayer
        3. 16.1.3. The sample tables
        4. 16.1.4. Improving the displayer
          1. 16.1.4.1. Displaying column headers
          2. 16.1.4.2. Error checking
          3. 16.1.4.3. Cosmetic issues
          4. 16.1.4.4. Displaying arbitrary queries
      2. 16.2. Complex Mappings
        1. 16.2.1. Multiple queries versus complex printing
        2. 16.2.2. A multiple-query example
        3. 16.2.3. A complex printing example
      3. 16.3. Creating the Sample Tables
      4. 16.4. Summary
    7. 17. Integrating Web Forms and Databases
      1. 17.1. HTML Forms
      2. 17.2. Basic Form Submission to a Database
      3. 17.3. Self-Submission
      4. 17.4. Editing Data with an HTML Form
        1. 17.4.1. TEXT and TEXTAREA
        2. 17.4.2. CHECKBOX
        3. 17.4.3. RADIO
        4. 17.4.4. SELECT
      5. 17.5. Summary
    8. 18. Improving Database Efficiency
      1. 18.1. Connections — Reduce, Reuse, Recycle
        1. 18.1.1. A bad example: one connection per statement
        2. 18.1.2. Multiple results don't need multiple connections
        3. 18.1.3. Persistent connections
      2. 18.2. Indexing and Table Design
        1. 18.2.1. Indexing
          1. 18.2.1.1. What is an index?
          2. 18.2.1.2. Indexing tradeoffs
          3. 18.2.1.3. Primary keys
        2. 18.2.2. Everything including the kitchen sink
        3. 18.2.3. Other types of indexes
          1. 18.2.3.1. UNIQUE
        4. 18.2.4. Table design
      3. 18.3. Making the Database Work for You
        1. 18.3.1. It's probably faster than you are
        2. 18.3.2. A bad example: looping, not restricting
          1. 18.3.2.1. Sorting and aggregating
          2. 18.3.2.2. Where possible, use MIN or MAX rather than sorting
        3. 18.3.3. Creating date and time fields
        4. 18.3.4. Finding the last inserted row
      4. 18.4. Summary
    9. 19. MySQL Gotchas
      1. 19.1. No Connection
      2. 19.2. Problems with Privileges
      3. 19.3. Unescaped Quotes
      4. 19.4. Broken SQL Statements
        1. 19.4.1. Misspelled names
        2. 19.4.2. Comma faults
        3. 19.4.3. Unquoted string arguments
        4. 19.4.4. Unbound variables
      5. 19.5. Too Little Data, Too Much Data
      6. 19.6. Specific SQL Functions
        1. 19.6.1. mysql_affected_rows() versus mysql_num_rows()
        2. 19.6.2. mysql_result()
        3. 19.6.3. OCI_Fetch()
      7. 19.7. Debugging and Sanity Checking
      8. 19.8. Summary
  8. III. More PHP
    1. 20. Introducing Object-Oriented PHP
      1. 20.1. What Is Object-Oriented Programming?
        1. 20.1.1. The simple idea
          1. 20.1.1.1. The procedural approach
          2. 20.1.1.2. The object-oriented approach
        2. 20.1.2. Elaboration: objects as data types
        3. 20.1.3. Elaboration: Inheritance
        4. 20.1.4. Elaboration: Encapsulation
        5. 20.1.5. Elaboration: Constructors and destructors
        6. 20.1.6. Terminology
      2. 20.2. Basic PHP Constructs for OOP
        1. 20.2.1. Defining classes
        2. 20.2.2. Accessing member variables
        3. 20.2.3. Creating instances
        4. 20.2.4. Constructor functions
        5. 20.2.5. Inheritance
        6. 20.2.6. Overriding functions
        7. 20.2.7. Chained subclassing
        8. 20.2.8. Modifying and assigning objects
        9. 20.2.9. Scoping issues
      3. 20.3. Advanced OOP Features
        1. 20.3.1. Public, Private, and Protected Members
          1. 20.3.1.1. Private members
          2. 20.3.1.2. Protected members
        2. 20.3.2. Interfaces
        3. 20.3.3. Constants
        4. 20.3.4. Abstract Classes
        5. 20.3.5. Simulating class functions
        6. 20.3.6. Calling parent functions
          1. 20.3.6.1. Calling parent constructors
        7. 20.3.7. Automatic calls to parent constructors
        8. 20.3.8. Simulating method overloading
        9. 20.3.9. Serialization
          1. 20.3.9.1. Sleeping and waking up
          2. 20.3.9.2. Serialization gotchas
      4. 20.4. Introspection Functions
        1. 20.4.1. Function overview
        2. 20.4.2. Example: Class genealogy
        3. 20.4.3. Example: matching variables and DB columns
        4. 20.4.4. Example: Generalized test methods
      5. 20.5. Extended Example: HTML Forms
      6. 20.6. Gotchas and Troubleshooting
        1. 20.6.1. Symptom: Member variable has no value in member function
        2. 20.6.2. Symptom: Parse error, expecting T_VARIABLE . . .
      7. 20.7. OOP Style in PHP
        1. 20.7.1. Naming conventions
        2. 20.7.2. Accessor functions
        3. 20.7.3. Designing for inheritance
      8. 20.8. Summary
    2. 21. Advanced Array Functions
      1. 21.1. Transformations of Arrays
        1. 21.1.1. Retrieving keys and values
        2. 21.1.2. Flipping, reversing, and shuffling
        3. 21.1.3. Merging, padding, slicing, and splicing
      2. 21.2. Stacks and Queues
      3. 21.3. Translating between Variables and Arrays
      4. 21.4. Sorting
      5. 21.5. Printing Functions for Visualizing Arrays
      6. 21.6. Summary
    3. 22. Examining Regular Expressions
      1. 22.1. Tokenizing and Parsing Functions
      2. 22.2. Why Regular Expressions?
        1. 22.2.1. Regex in PHP
        2. 22.2.2. An example of POSIX-style regex
        3. 22.2.3. Regular expression functions
      3. 22.3. Perl-Compatible Regular Expressions
      4. 22.4. Example: A simple link-scraper
        1. 22.4.1. The regular expression
        2. 22.4.2. Using the expression in a function
          1. 22.4.2.1. Applying the function
          2. 22.4.2.2. Extending the code
      5. 22.5. Advanced String Functions
        1. 22.5.1. HTML functions
        2. 22.5.2. Hashing using MD5
        3. 22.5.3. Strings as character collections
        4. 22.5.4. String similarity functions
      6. 22.6. Summary
    4. 23. Working with the Filesystem
      1. 23.1. Understanding PHP File Permissions
      2. 23.2. File Reading and Writing Functions
        1. 23.2.1. File open
          1. 23.2.1.1. HTTP fopen
          2. 23.2.1.2. FTP fopen
            1. 23.2.1.2.1. Standard I/O fopen
            2. 23.2.1.2.2. Filesystem fopen
        2. 23.2.2. File read
        3. 23.2.3. Constructing file downloads by using fpassthru()
        4. 23.2.4. File write
        5. 23.2.5. File close
      3. 23.3. Filesystem and Directory Functions
        1. 23.3.1. feof
        2. 23.3.2. file_exists
        3. 23.3.3. filesize
      4. 23.4. Network Functions
        1. 23.4.1. Syslog functions
        2. 23.4.2. DNS functions
        3. 23.4.3. Socket functions
      5. 23.5. Date and Time Functions
        1. 23.5.1. If you don't know either date or time
        2. 23.5.2. If you've already determined the date/time/timestamp
      6. 23.6. Calendar Conversion Functions
      7. 23.7. Summary
    5. 24. Working with Cookies and Sessions
      1. 24.1. What's a Session?
        1. 24.1.1. So what's the problem?
        2. 24.1.2. Why should you care?
      2. 24.2. Home-grown Alternatives
        1. 24.2.1. IP address
        2. 24.2.2. Hidden variables
        3. 24.2.3. Cookie-based home-grown sessions
      3. 24.3. How Sessions Work in PHP
        1. 24.3.1. Making PHP aware of your session
        2. 24.3.2. Propagating session variables
          1. 24.3.2.1. The simple approach (using $_SESSION)
        3. 24.3.3. Where is the data really stored?
      4. 24.4. Sample Session Code
      5. 24.5. Session Functions
      6. 24.6. Configuration Issues
      7. 24.7. Cookies
        1. 24.7.1. The setcookie() function
        2. 24.7.2. Examples
        3. 24.7.3. Deleting cookies
        4. 24.7.4. Reading cookies
        5. 24.7.5. Cookie pitfalls
          1. 24.7.5.1. Sending something else first
          2. 24.7.5.2. Reverse-order interpretation
          3. 24.7.5.3. Cookie refusal
      8. 24.8. Sending HTTP Headers
        1. 24.8.1. Example: Redirection
        2. 24.8.2. Example: HTTP authentication
        3. 24.8.3. Header gotchas
      9. 24.9. Gotchas and Troubleshooting
      10. 24.10. Summary
    6. 25. Learning PHP Types
      1. 25.1. Type Round-up
      2. 25.2. Resources
        1. 25.2.1. What are resources?
        2. 25.2.2. How to handle resources
      3. 25.3. Type Testing
      4. 25.4. Assignment and Coercion
        1. 25.4.1. Type conversion behavior
        2. 25.4.2. Explicit conversions
        3. 25.4.3. Conversion examples
        4. 25.4.4. Other useful type conversions
        5. 25.4.5. Integer overflow
        6. 25.4.6. Finding the largest integer
      5. 25.5. Summary
    7. 26. Learning PHP Advanced Functions
      1. 26.1. Variable Numbers of Arguments
        1. 26.1.1. Default arguments
        2. 26.1.2. Arrays as multiple-argument substitutes
        3. 26.1.3. Multiple arguments in PHP4 and above
      2. 26.2. Call-by-value
      3. 26.3. Call-by-reference
      4. 26.4. Variable function names
      5. 26.5. An extended example
      6. 26.6. Summary
    8. 27. Performing Math with PHP
      1. 27.1. Mathematical Constants
      2. 27.2. Tests on Numbers
      3. 27.3. Base Conversion
      4. 27.4. Exponents and Logarithms
      5. 27.5. Trigonometry
      6. 27.6. Arbitrary Precision (BC)
        1. 27.6.1. An arbitrary-precision example
        2. 27.6.2. Converting code to arbitrary-precision
      7. 27.7. Summary
    9. 28. Securing PHP
      1. 28.1. Possible Attacks
        1. 28.1.1. Site defacement
        2. 28.1.2. Accessing source code
        3. 28.1.3. Reading arbitrary files
        4. 28.1.4. Running arbitrary programs
        5. 28.1.5. Viruses and other e-critters
      2. 28.2. FYI: Security Web Sites
      3. 28.3. Summary
    10. 29. Learning PHP Configuration
      1. 29.1. Viewing Environment Variables
      2. 29.2. Understanding PHP Configuration
        1. 29.2.1. Compile-time options
          1. 29.2.1.1. --with-apache[=DIR] or --with-apache2=[DIR]
          2. 29.2.1.2. --with-apxs[=DIR] or --with-apxs2[=DIR]
          3. 29.2.1.3. --with-[database][=DIR]
          4. 29.2.1.4. --with-mcrypt[=DIR]
          5. 29.2.1.5. --with-java[=DIR]
          6. 29.2.1.6. --with-xmlrpc
          7. 29.2.1.7. --with-dom[=DIR]
          8. 29.2.1.8. --enable-bcmath
          9. 29.2.1.9. --enable-calendar
          10. 29.2.1.10. --with-config-file-path=DIR
          11. 29.2.1.11. --enable-url-includes
          12. 29.2.1.12. --disable-url-fopen-wrapper
        2. 29.2.2. CGI compile-time options
          1. 29.2.2.1. --with-exec-dir[=DIR]
          2. 29.2.2.2. --enable-discard-path
          3. 29.2.2.3. --enable-force-cgi-redirect
        3. 29.2.3. Apache configuration files
          1. 29.2.3.1. Timeout
          2. 29.2.3.2. DocumentRoot
          3. 29.2.3.3. AddType
          4. 29.2.3.4. Action
          5. 29.2.3.5. LoadModule
          6. 29.2.3.6. AddModule
        4. 29.2.4. The php.ini file
          1. 29.2.4.1. short_open_tag = Off
          2. 29.2.4.2. disable_functions = [function1, function2, function3 . . . functionn]
          3. 29.2.4.3. max_execution_time = 30
          4. 29.2.4.4. error_reporting = E_ALL & ~E_NOTICE
          5. 29.2.4.5. error_prepend_string = ["<font color=ff0000>"]
          6. 29.2.4.6. warn_plus_overloading = Off
          7. 29.2.4.7. variables_order = EGPCS
          8. 29.2.4.8. gpc_order = GPC
          9. 29.2.4.9. auto-prepend-file = [path/to/file]
          10. 29.2.4.10. auto-append-file = [path/to/file]
          11. 29.2.4.11. include_path = [DIR]
          12. 29.2.4.12. doc_root = [DIR]
          13. 29.2.4.13. upload_tmp_dir = [DIR]
          14. 29.2.4.14. session.save-handler = files
          15. 29.2.4.15. ignore_user_abort = [On/Off]
      3. 29.3. Improving PHP Performance
      4. 29.4. Summary
    11. 30. Handing Exceptions with PHP
      1. 30.1. Error Handling in PHP
        1. 30.1.1. Errors and exceptions
        2. 30.1.2. The Exception class
        3. 30.1.3. The try/catch block
        4. 30.1.4. Throwing an exception
        5. 30.1.5. Defining your own Exception subclasses
        6. 30.1.6. Limitations of Exceptions in PHP
      2. 30.2. Other Methods of Error Handling
        1. 30.2.1. Native PHP errors
        2. 30.2.2. Defining an error handler
        3. 30.2.3. Triggering a user error
      3. 30.3. Logging and Debugging
      4. 30.4. Summary
    12. 31. Debugging PHP Programs
      1. 31.1. General Troubleshooting Strategies
        1. 31.1.1. Change one thing at a time
        2. 31.1.2. Try to isolate the problem
        3. 31.1.3. Simplify, then build up
        4. 31.1.4. Check the obvious
        5. 31.1.5. Document your solution
        6. 31.1.6. After fixing, retest
      2. 31.2. A Menagerie of Bugs
        1. 31.2.1. Compile-time bugs
        2. 31.2.2. Runtime bugs
        3. 31.2.3. Logical bugs
      3. 31.3. Using Web Server Logs
        1. 31.3.1. Apache
          1. 31.3.1.1. The Common Log Format
          2. 31.3.1.2. HTTP response codes
          3. 31.3.1.3. Monitoring Apache logs with tail
        2. 31.3.2. IIS
      4. 31.4. PHP Error Reporting and Logging
        1. 31.4.1. Error reporting
        2. 31.4.2. Error logging
        3. 31.4.3. Choosing which errors to report or log
      5. 31.5. Error-Reporting Functions
        1. 31.5.1. Diagnostic print statements
        2. 31.5.2. Using var_dump()
        3. 31.5.3. Using syslog()
        4. 31.5.4. Logging to a custom location
        5. 31.5.5. Using error_log()
      6. 31.6. Summary
    13. 32. Learning PHP Style
      1. 32.1. The Uses of Style
      2. 32.2. Readability
        1. 32.2.1. Comments
        2. 32.2.2. PHPDoc
        3. 32.2.3. File and variable names
          1. 32.2.3.1. Long versus short
          2. 32.2.3.2. Underscores versus camelcaps
          3. 32.2.3.3. Reassigning variables
        4. 32.2.4. Uniformity of style
      3. 32.3. Maintainability
        1. 32.3.1. Avoid magic numbers
        2. 32.3.2. Functions
        3. 32.3.3. Include files
        4. 32.3.4. Object wrappers
        5. 32.3.5. Consider using version control
      4. 32.4. Robustness
        1. 32.4.1. Unavailability of service
        2. 32.4.2. Unexpected variable types
      5. 32.5. Efficiency and Conciseness
        1. 32.5.1. Efficiency: only the algorithm matters
        2. 32.5.2. Efficiency optimization tips
          1. 32.5.2.1. Don't reinvent the wheel
          2. 32.5.2.2. Discover the bottleneck
          3. 32.5.2.3. Focus on database queries
          4. 32.5.2.4. Focus on the innermost loop
        3. 32.5.3. Conciseness: the downside
          1. 32.5.3.1. Conciseness rarely implies efficiency
          2. 32.5.3.2. Conciseness trades off with readability
        4. 32.5.4. Conciseness tips
          1. 32.5.4.1. Use return values and side effects at the same time
          2. 32.5.4.2. Use incrementing and assignment operators
          3. 32.5.4.3. Reuse functions
          4. 32.5.4.4. There's nothing wrong with Boolean
          5. 32.5.4.5. Use short-circuiting Boolean expressions
      6. 32.6. HTML Mode or PHP Mode?
        1. 32.6.1. Minimal PHP
        2. 32.6.2. Maximal PHP
        3. 32.6.3. Medium PHP
        4. 32.6.4. The heredoc style
      7. 32.7. Separating Code from Design
        1. 32.7.1. Functions
        2. 32.7.2. Cascading style sheets in PHP
        3. 32.7.3. Templates and page consistency
      8. 32.8. Summary
  9. IV. Other Databases
    1. 33. Connecting PHP and PostgreSQL
      1. 33.1. Why Choose PostgreSQL?
      2. 33.2. Why Object-Relational Anyway?
        1. 33.2.1. But is it a database yet?
      3. 33.3. Down to Real Work
      4. 33.4. PHP and PostgreSQL
      5. 33.5. The Cartoons Database
      6. 33.6. Summary
    2. 34. Using PEAR DB with PHP
      1. 34.1. Pear DB Concepts
        1. 34.1.1. Data Source Names (DSNs)
        2. 34.1.2. Connection
        3. 34.1.3. Query
        4. 34.1.4. Row retrieval
        5. 34.1.5. Disconnection
        6. 34.1.6. A complete example
      2. 34.2. PEAR DB Functions
        1. 34.2.1. Members of the DB class
        2. 34.2.2. Members of the DB_Common class
        3. 34.2.3. Members of the DB_Result class
      3. 34.3. Summary
    3. 35. An Overview of Oracle
      1. 35.1. When Do You Need Oracle?
        1. 35.1.1. Money
        2. 35.1.2. Other rivalrous resources
        3. 35.1.3. Huge data sets
        4. 35.1.4. Lots of big formulaic writes or data munging
        5. 35.1.5. Triggers
        6. 35.1.6. Legal liability
        7. 35.1.7. Bottom line: two-year outlook
      2. 35.2. Oracle and Web Architecture
        1. 35.2.1. Specialized team members
        2. 35.2.2. Shared development databases
        3. 35.2.3. Limited schema changes
        4. 35.2.4. Tools (or lack thereof)
        5. 35.2.5. Replication and failover
        6. 35.2.6. Data caching
      3. 35.3. Using OCI8 Functions
        1. 35.3.1. Escaping strings
        2. 35.3.2. Parsing and executing
        3. 35.3.3. Error reporting
        4. 35.3.4. Memory management
        5. 35.3.5. Ask for nulls
        6. 35.3.6. Fetching entire data sets
        7. 35.3.7. All caps
        8. 35.3.8. Transactionality
        9. 35.3.9. Stored procedures and cursors
      4. 35.4. Project: Point Editor
      5. 35.5. Project: Batch Editor
      6. 35.6. Summary
    4. 36. An Introduction to SQLite
      1. 36.1. An Introduction to SQLite
      2. 36.2. Using SQLite-related Functions
        1. 36.2.1. Creating Databases
        2. 36.2.2. Running Queries
          1. 36.2.2.1. Creating Tables
          2. 36.2.2.2. Inserting Data
          3. 36.2.2.3. Fetching Data
      3. 36.3. More on SQLite
      4. 36.4. Summary
  10. V. Connections
    1. 37. Sending E-Mail with PHP
      1. 37.1. Sending E-Mail with PHP
        1. 37.1.1. Windows configuration
        2. 37.1.2. Linux configuration
        3. 37.1.3. The mail function
      2. 37.2. Sending Mail from a Form
      3. 37.3. Summary
    2. 38. Integrating PHP and Java
      1. 38.1. PHP for Java programmers
        1. 38.1.1. Similarities
          1. 38.1.1.1. Syntax
          2. 38.1.1.2. Operators
          3. 38.1.1.3. Object model
          4. 38.1.1.4. Memory management
          5. 38.1.1.5. Packages and libraries
        2. 38.1.2. Differences
          1. 38.1.2.1. Compiled versus scripting
          2. 38.1.2.2. Variable declaration and loose typing
        3. 38.1.3. Java Server Pages and PHP
          1. 38.1.3.1. Embedded HTML
          2. 38.1.3.2. Choose your scripting language
      2. 38.2. Integrating PHP and Java
        1. 38.2.1. The Java SAPI
          1. 38.2.1.1. Installation and setup
          2. 38.2.1.2. Further information
        2. 38.2.2. The Java extension
          1. 38.2.2.1. Installation and setup
          2. 38.2.2.2. Testing
        3. 38.2.3. The Java object
        4. 38.2.4. Errors and exceptions
        5. 38.2.5. Potential gotchas
          1. 38.2.5.1. Installation problems
          2. 38.2.5.2. It's the classpath, stupid
          3. 38.2.5.3. Here comes that loose typing again
          4. 38.2.5.4. Speed
        6. 38.2.6. The sky's the limit
      3. 38.3. Summary
    3. 39. Integrating PHP and JavaScript
      1. 39.1. Outputting JavaScript with PHP
        1. 39.1.1. Dueling objects
        2. 39.1.2. PHP doesn't care what it outputs
        3. 39.1.3. Where to use JavaScript
      2. 39.2. PHP as a Backup for JavaScript
      3. 39.3. Static versus Dynamic JavaScript
        1. 39.3.1. Dynamically generated forms
        2. 39.3.2. Passing data back to PHP from JavaScript
      4. 39.4. Summary
    4. 40. Integrating PHP and XML
      1. 40.1. What Is XML?
      2. 40.2. Working with XML
      3. 40.3. Documents and DTDs
        1. 40.3.1. The structure of a DTD
        2. 40.3.2. Validating and nonvalidating parsers
      4. 40.4. SAX versus DOM
      5. 40.5. DOM
        1. 40.5.1. Using DOM XML
        2. 40.5.2. DOM functions
      6. 40.6. SAX
        1. 40.6.1. Using SAX
        2. 40.6.2. SAX options
        3. 40.6.3. SAX functions
      7. 40.7. SimpleXML API
        1. 40.7.1. Using SimpleXML
        2. 40.7.2. SimpleXML functions
      8. 40.8. A Sample XML Application
      9. 40.9. Gotchas and Troubleshooting
      10. 40.10. Summary
    5. 41. Creating and Consuming Web Services with PHP
      1. 41.1. The End of Programming as We Know It
        1. 41.1.1. The ugly truth about data movement
        2. 41.1.2. Brutal simplicity
      2. 41.2. REST, XML-RPC, SOAP, .NET
        1. 41.2.1. REST
        2. 41.2.2. SOAP
      3. 41.3. Current Issues with Web Services
        1. 41.3.1. Large Footprint
        2. 41.3.2. Potentially heavy load
        3. 41.3.3. Standards
        4. 41.3.4. Hide and seek
        5. 41.3.5. Who pays and how?
      4. 41.4. Project: A REST Client
      5. 41.5. Summary
    6. 42. Creating Graphics with PHP
      1. 42.1. Your Options
      2. 42.2. HTML Graphics
      3. 42.3. Creating images using gd
        1. 42.3.1. What is gd?
        2. 42.3.2. Image formats and browsers
        3. 42.3.3. Installation
        4. 42.3.4. gd Concepts
          1. 42.3.4.1. Colors
            1. 42.3.4.1.1. Palette-based images
            2. 42.3.4.1.2. Truecolor
            3. 42.3.4.1.3. Transparency
          2. 42.3.4.2. Drawing coordinates and commands
          3. 42.3.4.3. Format translation
          4. 42.3.4.4. Freeing resources
        5. 42.3.5. Functions
        6. 42.3.6. Images and HTTP
          1. 42.3.6.1. Full-page images
          2. 42.3.6.2. Embedded images from files
          3. 42.3.6.3. Embedded images from scripts
        7. 42.3.7. Example: fractal images
      4. 42.4. Gotchas and Troubleshooting
        1. 42.4.1. Symptom: completely blank image
        2. 42.4.2. Symptom: headers already sent
        3. 42.4.3. Symptom: broken image
      5. 42.5. Summary
  11. VI. Case Studies
    1. 43. Developing a Weblog with PHP
      1. 43.1. Why Weblogs?
      2. 43.2. The Simplest Weblog
      3. 43.3. Adding an HTML-Editing Tool
      4. 43.4. Changes and Additions
      5. 43.5. Summary
    2. 44. A Trivia Game
      1. 44.1. Concepts Used in This Chapter
      2. 44.2. The Game
        1. 44.2.1. Our version
        2. 44.2.2. Sample screens
        3. 44.2.3. The rules
        4. 44.2.4. Playing the game yourself
      3. 44.3. The Code
        1. 44.3.1. Code files
          1. 44.3.1.1. index.php
            1. 44.3.1.1.1. Exceptions
          2. 44.3.1.2. game_display_class.php
          3. 44.3.1.3. game_text_class.php
          4. 44.3.1.4. game_class.php
            1. 44.3.1.4.1. Data members
            2. 44.3.1.4.2. Public functions
            3. 44.3.1.4.3. Database interaction
            4. 44.3.1.4.4. Handling an answer
            5. 44.3.1.4.5. Serialization and sleep()
          5. 44.3.1.5. game_parameters_class.php
          6. 44.3.1.6. certainty_utils.php
          7. 44.3.1.7. question_class.php
          8. 44.3.1.8. dbvars.php
        2. 44.3.2. Creating the database
          1. 44.3.2.1. Table definitions
          2. 44.3.2.2. entry_form.php
      4. 44.4. General Design Considerations
        1. 44.4.1. Separation of code and display
        2. 44.4.2. Persistence of data
        3. 44.4.3. Exception handling
      5. 44.5. Summary
    3. 45. Data Visualization with Venn Diagrams
      1. 45.1. Scaled Venn diagrams
        1. 45.1.1. The task
      2. 45.2. Outline of the code
      3. 45.3. Necessary Trigonometry
      4. 45.4. Planning the Display
        1. 45.4.1. Simplifying assumptions
        2. 45.4.2. Determining size and scale
          1. 45.4.2.1. The easy cases
          2. 45.4.2.2. The hard case
      5. 45.5. Display
        1. 45.5.1. Notes on circles
        2. 45.5.2. Notes on centering text
      6. 45.6. Visualizing a Database
        1. 45.6.1. Trying it out
      7. 45.7. Extensions
      8. 45.8. Summary
  12. A. PHP for C Programmers
    1. A.1. Similarities
      1. A.1.1. Syntax
      2. A.1.2. Operators
      3. A.1.3. Control structures
      4. A.1.4. Many function names
    2. A.2. Differences
      1. A.2.1. Those dollar signs
      2. A.2.2. Types
      3. A.2.3. Type conversion
      4. A.2.4. Arrays
      5. A.2.5. No structure type
      6. A.2.6. Objects
      7. A.2.7. No pointers
      8. A.2.8. No prototypes
      9. A.2.9. Memory management
      10. A.2.10. Compilation and linking
      11. A.2.11. Permissiveness
    3. A.3. Guide to the Book
    4. A.4. A Bonus: Just Look at the Code!
  13. B. PHP for Perl Hackers
    1. B.1. Similarities
      1. B.1.1. Compiled scripting languages
      2. B.1.2. Syntax
      3. B.1.3. Dollar-sign variables
      4. B.1.4. No declaration of variables
      5. B.1.5. Loose typing of variables
      6. B.1.6. Strings and variable interpolation
    2. B.2. Differences
      1. B.2.1. PHP is HTML-embedded
      2. B.2.2. No @ or % variables
      3. B.2.3. Arrays versus hashes
      4. B.2.4. Specifying arguments to functions
      5. B.2.5. Variable scoping in functions
      6. B.2.6. No module system as such
      7. B.2.7. Break and continue rather than next and last
      8. B.2.8. No elsif
      9. B.2.9. More kinds of comments
      10. B.2.10. Regular expressions
    3. B.3. Miscellaneous Tips
      1. B.3.1. What about use of strict "vars"?
      2. B.3.2. Where's CPAN?
    4. B.4. Guide to the Book
  14. C. PHP for HTML Coders
    1. C.1. The Good News
      1. C.1.1. You already know HTML
      2. C.1.2. PHP is an easy first programming language to learn
      3. C.1.3. Web development is increasingly prefab anyway
    2. C.2. The Bad News
      1. C.2.1. If programming were that easy, you'd already know how
      2. C.2.2. Backend servers can add complexity
    3. C.3. Concentrate On . . .
      1. C.3.1. Reading other people's code
      2. C.3.2. Working on what interests you
      3. C.3.3. Thinking about programming
      4. C.3.4. Learning SQL and other protocols
      5. C.3.5. Making cosmetic changes to prefab PHP applications
      6. C.3.6. Debugging is programming
    4. C.4. Avoid at First . . .
      1. C.4.1. Maximal PHP style
      2. C.4.2. Programming large applications from scratch
    5. C.5. Consider This . . .
      1. C.5.1. Reading a book on C programming
      2. C.5.2. Minimal PHP style
      3. C.5.3. Use the right tools for the job
  15. D. PHP Resources
    1. D.1. The PHP Web Site
    2. D.2. The PHP Mailing Lists
      1. D.2.1. Signing up
      2. D.2.2. Users' lists and developers' lists
      3. D.2.3. Regular and digest
      4. D.2.4. Mailing list etiquette
        1. D.2.4.1. Remember, the community does all this work for free!
        2. D.2.4.2. People might be sick of your question
        3. D.2.4.3. Give detailed descriptions
        4. D.2.4.4. PHP is international
        5. D.2.4.5. There are limits
        6. D.2.4.6. Do it yourself
        7. D.2.4.7. It's probably you
        8. D.2.4.8. There are now commercial alternatives
    3. D.3. Other PHP Web Sites
      1. D.3.1. Core scripting engine and tools
      2. D.3.2. PHP knowledgebase
      3. D.3.3. Articles and tutorials
      4. D.3.4. PHP codebases
      5. D.3.5. Major PHP projects
  16. E. PEAR
    1. E.1. What Is PEAR?
    2. E.2. The PEAR Package System
      1. E.2.1. A sampling of PEAR packages
      2. E.2.2. How the PEAR database works
      3. E.2.3. The Package Manager
        1. E.2.3.1. Installing the PEAR Package Manager on Linux
        2. E.2.3.2. Updating the Package Manager
      4. E.2.4. Using the Manager
        1. E.2.4.1. Automatic package installation
        2. E.2.4.2. Automatic package removal
        3. E.2.4.3. Semiautomatic package installation
        4. E.2.4.4. Using PEAR packages in your scripts
    3. E.3. PHP Foundation Classes (PFC)
    4. E.4. PHP Extension Code Library (PECL)
    5. E.5. The PEAR Coding Style
      1. E.5.1. Indenting, whitespace, and line length
      2. E.5.2. Formatting control structures
        1. E.5.2.1. if Statements
        2. E.5.2.2. if/else Statements
        3. E.5.2.3. if/elseif Statements
        4. E.5.2.4. switch Statements
      3. E.5.3. Formatting functions and function calls
    6. E.6. Summary

Product information

  • Title: PHP6 and MySQL® 6 Bible
  • Author(s): Tim Converse, Steve Suehring, Joyce Park
  • Release date: January 2009
  • Publisher(s): Wiley
  • ISBN: 9780470384503