You are previewing PHP in a Nutshell.
O'Reilly logo
PHP in a Nutshell

Book Description

Now installed on more than 20 million Internet domains around the world, PHP is an undisputed leader in web programming languages. Database connectivity, powerful extensions, and rich object-orientation are all reasons for its popularity, but nearly everyone would agree that, above all, PHP is one of the easiest languages to learn and use for developing dynamic web applications. The ease of development and simplicity of PHP, combined with a large community and expansive repository of open source PHP libraries, make it a favorite of web designers and developers worldwide.

PHP in a Nutshell is a complete reference to the core of the language as well as the most popular PHP extensions. This book doesn't try to compete with or replace the widely available online documentation. Instead, it is designed to provide depth and breadth that can't be found elsewhere. PHP in a Nutshell provides the maximum information density on PHP, without all the fluff and extras that get in the way. The topic grouping, tips, and examples in this book complement the online guide and make this an essential reference for every PHP programmer. This book focuses on the functions commonly used by a majority of developers, so you can look up the information you need quickly. Topics include:

  • Object-oriented PHP

  • Networking

  • String manipulation

  • Working with files

  • Database interaction

  • XML

  • Multimedia creation

  • Mathematics

  • Whether you're just getting started or have years of experience in PHP development, PHP in a Nutshell is a valuable addition to your desk library.

    Table of Contents

    1. PHP in a Nutshell
      1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
      2. About the Author
      3. Preface
        1. Audience
        2. Assumptions
        3. Contents of This Book
        4. Conventions Used in This Book
        5. Using Code Examples
        6. Safari® Enabled
        7. How to Contact Us
        8. Acknowledgments
      4. 1. Introduction to PHP
        1. 1.1. PHP History
        2. 1.2. Advantages of PHP
          1. 1.2.1. The HTML Relationship
          2. 1.2.2. Interpreting Versus Compiling
          3. 1.2.3. Output Control
          4. 1.2.4. Performance
        3. 1.3. Getting Help
          1. 1.3.1. The Documentation
          2. 1.3.2. Mailing Lists
          3. 1.3.4. IRC
          4. 1.3.5. Conferences
          5. 1.3.6. User Groups
          6. 1.3.7. Submitting a Bug
        4. 1.4. Getting Certified
        5. 1.5. PHP Resources
          1. 1.5.1. Books
          2. 1.5.2. Magazines
          3. 1.5.3. Web Sites
      5. 2. Installing PHP
        1. 2.1. Installing on Windows
          1. 2.1.1. Installing Apache
          2. 2.1.2. Installing Microsoft IIS
          3. 2.1.3. Configuring Extensions
        2. 2.2. Installing on Unix
          1. 2.2.1. Installing Using Packages
          2. 2.2.2. Compiling from Source
          3. 2.2.3. Configuring Extensions
        3. 2.3. Testing Your Configuration
        4. 2.4. System Configuration
      6. 3. The PHP Interpreter
        1. 3.1. Running PHP Scripts
        2. 3.2. Extending PHP
        3. 3.3. PEAR
        4. 3.4. Abnormal Script Termination
      7. 4. The PHP Language
        1. 4.1. The Basics of PHP
        2. 4.2. Variables
        3. 4.3. Whitespace
        4. 4.4. Heredoc
        5. 4.5. Brief Introduction to Variable Types
        6. 4.6. Code Blocks
        7. 4.7. Opening and Closing Code Islands
        8. 4.8. Comments
        9. 4.9. Conditional Statements
        10. 4.10. Case Switching
        11. 4.11. Loops
        12. 4.12. Infinite Loops
        13. 4.13. Special Loop Keywords
        14. 4.14. Loops Within Loops
        15. 4.15. Mixed-Mode Processing
        16. 4.16. Including Other Files
        17. 4.17. Functions
          1. 4.17.1. A Simple User Function
          2. 4.17.2. Return Values
          3. 4.17.3. Parameters
          4. 4.17.4. Passing By Reference
          5. 4.17.5. Returning by Reference
          6. 4.17.6. Default Parameters
          7. 4.17.7. Variable Parameter Counts
          8. 4.17.8. Variable Scope in Functions
          9. 4.17.9. Overriding Scope with the GLOBALS Array
          10. 4.17.10. Recursive Functions
      8. 5. Variables and Constants
        1. 5.1. Types of Data
        2. 5.2. True or False
        3. 5.3. Strings
          1. 5.3.1. Escape Sequences
        4. 5.4. Integers
        5. 5.5. Floats
        6. 5.6. Automatic Type Conversion
        7. 5.7. Checking Whether a Variable Is Set: isset()
        8. 5.8. Variable Scope
        9. 5.9. Variable Variables
        10. 5.10. Superglobals
        11. 5.11. Using $_ENV and $_SERVER
        12. 5.12. References
        13. 5.13. Constants
          1. 5.13.1. Preset Constants
          2. 5.13.2. Mathematical Constants
        14. 5.14. Arrays
          1. 5.14.1. First Steps
          2. 5.14.2. Associative Arrays
          3. 5.14.3. The Array Operator
          4. 5.14.4. Returning Arrays from Functions
          5. 5.14.5. Array-Specific Functions
            1. array_diff()
            2. array_filter()
            3. array_flip()
            4. array_intersect()
            5. array_keys()
            6. array_merge()
            7. array_pop()
            8. array_push()
            9. array_rand()
            10. array_shift()
            11. array_unique()
            12. array_unshift()
            13. array_values()
            14. arsort()
            15. asort()
            16. explode()
            17. extract()
            18. implode()
            19. in_array()
            20. krsort()
            21. ksort()
            22. range()
            23. shuffle()
          6. 5.14.6. Multidimensional Arrays
          7. 5.14.7. The Array Cursor
          8. 5.14.8. Holes in Arrays
          9. 5.14.9. Using Arrays in Strings
          10. 5.14.10. Saving Arrays
      9. 6. Operators
        1. 6.1. Arithmetic Operators
        2. 6.2. Assignment Operators
        3. 6.3. String Operators
        4. 6.4. Bitwise Operators
        5. 6.5. Comparison Operators
        6. 6.6. Incrementing and Decrementing Operators
        7. 6.7. Logical Operators
        8. 6.8. Some Operator Examples
        9. 6.9. The Ternary Operator
        10. 6.10. The Execution Operator
        11. 6.11. Operator Precedence and Associativity
      10. 7. Function Reference
        1. 7.1. Undocumented Functions
        2. 7.2. Handling Non-English Characters
          1. abs()
          2. acos()
          3. addslashes()
          4. asin()
          5. atan()
          6. base_convert()
          7. bindec()
          8. call_user_func()
          9. call_user_func_array()
          10. ceil()
          11. chr()
          12. connection_status()
          13. cos()
          14. count_chars()
          15. date()
          16. decbin()
          17. dechex()
          18. decoct()
          19. deg2rad()
          20. die()
          21. dl()
          22. empty()
          23. escapeshellcmd()
          24. eval()
          25. exec()
          26. exit()
          27. floor()
          28. function_exists()
          29. get_extension_funcs()
          30. get_loaded_extensions()
          31. hexdec()
          32. html_entities()
          33. html_entity_decode()
          34. ignore_user_abort()
          35. ini_get()
          36. ini_set()
          37. is_callable()
          38. isset()
          39. ltrim()
          40. md5()
          41. microtime()
          42. mktime()
          43. mt_rand()
          44. nl2br()
          45. number_format()
          46. octdec()
          47. ord()
          48. parse_str()
          49. passthru()
          50. pow()
          51. printf()
          52. rad2deg()
          53. rand()
          54. rawurldecode()
          55. rawurlencode()
          56. register_shutdown_function()
          57. round()
          58. rtrim()
          59. set_time_limit()
          60. sha1()
          61. sin()
          62. sleep()
          63. sqrt()
          64. str_pad()
          65. str_replace()
          66. str_word_count()
          67. strcasecmp()
          68. strcmp()
          69. strip_tags()
          70. stripslashes()
          71. strlen()
          72. strpos()
          73. strstr()
          74. strtolower()
          75. strtotime()
          76. strtoupper()
          77. substr()
          78. tan()
          79. time()
          80. trim()
          81. ucfirst()
          82. ucwords()
          83. unset()
          84. usleep()
          85. virtual()
          86. wordwrap()
      11. 8. Object-Oriented PHP
        1. 8.1. Conceptual Overview
        2. 8.2. Classes
          1. 8.2.1. Defining a Class
          2. 8.2.2. How to Design Your Class
          3. 8.2.3. Basic Inheritance
          4. 8.2.4. Overriding Methods
          5. 8.2.5. The Scope Resolution Operator
        3. 8.3. Objects
        4. 8.4. Properties
        5. 8.5. The 'this' Variable
        6. 8.6. Objects Within Objects
        7. 8.7. Access Control Modifiers
          1. 8.7.1. Public
          2. 8.7.2. Private
          3. 8.7.3. Protected
          4. 8.7.4. Final
          5. 8.7.5. Abstract
          6. 8.7.6. Iterating Through Object Properties
        8. 8.8. Object Type Information
        9. 8.9. Class Type Hints
        10. 8.10. Constructors and Destructors
          1. 8.10.1. Parent Constructors
          2. 8.10.2. Destructors
          3. 8.10.3. Deleting Objects
        11. 8.11. Copying Objects
        12. 8.12. Comparing Objects with == and ===
        13. 8.13. Saving Objects
        14. 8.14. Magic Methods
          1. 8.14.1. _ _autoload()
          2. 8.14.2. _ _get()
          3. 8.14.3. _ _set()
          4. 8.14.4. _ _call()
          5. 8.14.5. _ _toString()
        15. 8.15. Static Class Methods and Properties
        16. 8.16. Helpful Utility Functions
        17. 8.17. Interfaces
        18. 8.18. Dereferencing Object Return Values
      12. 9. HTML Forms
        1. 9.1. What Does It Mean to Be Dynamic?
        2. 9.2. Designing a Form
          1. 9.2.1. GET and POST
          2. 9.2.2. Available Elements
          3. 9.2.3. A Working Form
        3. 9.3. Handling Data
          1. 9.3.1. register_globals
          2. 9.3.2. Working Around register_globals
          3. 9.3.3. Magic Quotes
          4. 9.3.4. Handling Our Form
        4. 9.4. Splitting Forms Across Pages
        5. 9.5. Validating Input
        6. 9.6. Form Design
        7. 9.7. Summary
      13. 10. Cookies and Sessions
        1. 10.1. Cookies Versus Sessions
        2. 10.2. Using Cookies
        3. 10.3. Using Sessions
          1. 10.3.1. Starting a Session
          2. 10.3.2. Adding Session Data
          3. 10.3.3. Reading Session Data
          4. 10.3.4. Removing Session Data
          5. 10.3.5. Ending a Session
          6. 10.3.6. Checking Session Data
          7. 10.3.7. Files Versus Databases
        4. 10.4. Storing Complex Data Types
      14. 11. Output Buffering
        1. 11.1. Why Use Output Buffering?
        2. 11.2. Getting Started
        3. 11.3. Reusing Buffers
        4. 11.4. Stacking Buffers
        5. 11.5. Flushing Stacked Buffers
        6. 11.6. Reading Buffers
        7. 11.7. Other OB Functions
        8. 11.8. Flushing Output
        9. 11.9. Compressing Output
        10. 11.10. URL Rewriting
      15. 12. Security
        1. 12.1. Security Tips
          1. 12.1.1. Put Key Files Outside Your Document Root
          2. 12.1.2. Remember That Most Files Are Public
          3. 12.1.3. Hide Your Identity
          4. 12.1.4. Hiding PHP
        2. 12.2. Encryption
          1. 12.2.1. Encrypting Data
          2. 12.2.2. Symmetric Decryption
      16. 13. Files
        1. 13.1. Reading Files
          1. 13.1.1. readfile()
          2. 13.1.2. file_get_contents() and file()
          3. 13.1.3. fopen() and fread()
          4. 13.1.4. Reading by line using fgets()
        2. 13.2. Creating and Changing Files
          1. 13.2.1. file_put_contents()
          2. 13.2.2. fwrite()
        3. 13.3. Moving, Copying, and Deleting Files
          1. 13.3.1. Moving Files with rename()
          2. 13.3.2. Copying Files with copy()
          3. 13.3.3. Deleting Files with unlink()
        4. 13.4. Other File Functions
        5. 13.5. Checking Whether a File Exists
        6. 13.6. Retrieving File Time Information
        7. 13.7. Dissecting Filename Information
        8. 13.8. Handling File Uploads
          1. 13.8.1. Checking Uploaded Files
        9. 13.9. Locking Files with flock()
        10. 13.10. Reading File Permissions and Status
        11. 13.11. Changing File Permissions and Ownership
        12. 13.12. Working with Links
        13. 13.13. Working with Directories
          1. 13.13.1. Creating Directories
          2. 13.13.2. Deleting Directories
          3. 13.13.3. Reading and Changing the Working Directory
          4. 13.13.4. One Last Directory Function
        14. 13.14. Remote Files
        15. 13.15. File Checksums
        16. 13.16. Parsing a Configuration File
      17. 14. Databases
        1. 14.1. Using MySQL with PHP
          1. 14.1.1. Connecting to a MySQL Database
          2. 14.1.2. Querying and Formatting
          3. 14.1.3. Disconnecting from a MySQL Database
          4. 14.1.4. Reading in Data
          5. 14.1.5. Mixing in PHP Variables
          6. 14.1.6. Reading Auto-Incrementing Values
          7. 14.1.7. Unbuffered Queries for Large Data Sets
        2. 14.2. PEAR::DB
          1. 14.2.1. Quick PEAR::DB Calls
          2. 14.2.2. Query Information
          3. 14.2.3. Advanced PEAR::DB: Prepared Statements
        3. 14.3. SQLite
          1. 14.3.1. Before You Begin
          2. 14.3.2. Getting Started
          3. 14.3.3. Advanced Functions
          4. 14.3.4. Mixing SQLite and PHP
        4. 14.4. Persistent Connections
        5. 14.5. MySQL Improved
      18. 15. Regular Expressions
        1. 15.1. Basic Regexps with preg_match() and preg_match_all()
        2. 15.2. Regexp Character Classes
        3. 15.3. Regexp Special Characters
        4. 15.4. Words and Whitespace Regexps
        5. 15.5. Storing Matched Strings
        6. 15.6. Regular Expression Replacements
        7. 15.7. Regular Expression Syntax Examples
        8. 15.8. The Regular Expressions Coach
      19. 16. Manipulating Images
        1. 16.1. Getting Started
        2. 16.2. Choosing a Format
        3. 16.3. Getting Arty
        4. 16.4. More Shapes
        5. 16.5. Complex Shapes
        6. 16.6. Outputting Text
        7. 16.7. Loading Existing Images
        8. 16.8. Color and Image Fills
        9. 16.9. Adding Transparency
        10. 16.10. Using Brushes
        11. 16.11. Basic Image Copying
        12. 16.12. Scaling and Rotating
        13. 16.13. Points and Lines
        14. 16.14. Special Effects Using imagefilter()
        15. 16.15. Interlacing an Image
        16. 16.16. Getting an Image's MIME Type
      20. 17. Creating PDFs
        1. 17.1. Getting Started
        2. 17.2. Adding More Pages and More Style
        3. 17.3. Adding Images
        4. 17.4. PDF Special Effects
        5. 17.5. Adding Document Data
      21. 18. Creating Flash
        1. 18.1. A Simple Movie
        2. 18.2. Flash Text
        3. 18.3. Actions
        4. 18.4. Animation
      22. 19. XML & XSLT
        1. 19.1. SimpleXML
          1. 19.1.1. XML Attributes
          2. 19.1.2. Reading from a String
          3. 19.1.3. Searching and Filtering with XPath
          4. 19.1.4. Outputting XML
        2. 19.2. Transforming XML Using XSLT
          1. 19.2.1. An Example XSL Document
          2. 19.2.2. Adding PHP to the Mix
      23. 20. Network Programming
        1. 20.1. Sockets
          1. 20.1.1. Sockets Are Files
          2. 20.1.2. Creating a Server
        2. 20.2. HTTP
          1. 20.2.1. Sending Custom Headers
          2. 20.2.2. Reading Queued Headers
          3. 20.2.3. Authentication Over HTTP
        3. 20.3. Sending Mail
          1. 20.3.1. MIME Types
          2. 20.3.2. Easier Mail Sending with PEAR::Mail
          3. 20.3.3. Sending Mixed-Type Messages with PEAR::Mail_Mime
          4. 20.3.4. Sending Real Attachments
        4. 20.4. Curl
          1. 20.4.1. Installing Curl
          2. 20.4.2. Your First Curl Script
          3. 20.4.3. Trapping Return Values
          4. 20.4.4. Using FTP to Send Data
          5. 20.4.5. Sending Data Over HTTP
          6. 20.4.6. The Abridged List of Curl Options
          7. 20.4.7. Debugging Curl
      24. 21. Distributing Your Code
        1. 21.1. Cross-Platform Code 1: Loading Extensions
        2. 21.2. Cross-Platform Code 2: Using Extensions
        3. 21.3. Cross-Platform Code 3: Path and Line Separators
        4. 21.4. Cross-Platform Code 4: Coping with php.ini Differences
        5. 21.5. Cross-Platform Code 5: Checking the PHP Version with phpversion() and version_compare()
      25. 22. Debugging
        1. 22.1. The Most Basic Debugging Technique
        2. 22.2. Making Assertions
        3. 22.3. Triggering Your Own Errors
        4. 22.4. Testing with php_check_syntax()
        5. 22.5. Source Highlighting
        6. 22.6. Handling MySQL Errors
        7. 22.7. Exception Handling
        8. 22.8. Backtracing Your Code
        9. 22.9. Custom Error Handlers
        10. 22.10. Custom Exception Handlers
        11. 22.11. Using @ to Disable Errors
        12. 22.12. phpinfo()
        13. 22.13. Output Style
          1. 22.13.1. Installing Tidy
      26. 23. Performance
        1. 23.1. Write Your Code Sensibly
        2. 23.2. Use the Zend Optimizer
        3. 23.3. Use a PHP Code Cache
        4. 23.4. Compress Your Output
        5. 23.5. Don't Use CGI
        6. 23.6. Debug Your Code
        7. 23.7. Use Persistent Connections
        8. 23.8. Compile Right
      27. About the Author
      28. Colophon
      29. SPECIAL OFFER: Upgrade this ebook with O’Reilly