You are previewing Learning PHP.
O'Reilly logo
Learning PHP

Book Description

If you want to get started with PHP, this book is essential. Author David Sklar (PHP Cookbook) guides you through aspects of the language you need to build dynamic server-side websites. By exploring features of PHP 5.x and the exciting enhancements in the latest release, PHP 7, you’ll learn how to work with web servers, browsers, databases, and web services. End-of-chapter exercises help you make the lessons stick.

Whether you’re a hobbyist looking to build dynamic websites, a frontend developer ready to add server-side programs, or an experienced programmer who wants to get up to speed with this language, this gentle introduction also covers aspects of modern PHP, such as internationalization, using PHP from the command line, and package management.

  • Learn how PHP interacts with browsers and servers
  • Understand data types, variables, logic, looping, and other language basics
  • Explore how to use arrays, functions, and objects
  • Build and validate web forms
  • Work with databases and session management
  • Access APIs to interact with web services and other websites
  • Jumpstart your project with popular PHP web application frameworks

Table of Contents

  1. Preface
    1. Who This Book Is For
    2. Contents of This Book
    3. What’s Not in This Book
    4. Other Resources
    5. Conventions Used in This Book
      1. Programming Conventions
      2. Typographical Conventions
    6. Using Code Examples
    7. Safari® Books Online
    8. Comments and Questions
    9. Acknowledgments
  2. 1. Orientation and First Steps
    1. PHP’s Place in the Web World
    2. What’s So Great About PHP?
      1. PHP Is Free (as in Money)
      2. PHP Is Free (as in Speech)
      3. PHP Is Cross-Platform
      4. PHP Is Widely Used
      5. PHP Hides Its Complexity
      6. PHP Is Built for Web Programming
    3. PHP in Action
    4. Basic Rules of PHP Programs
      1. Start and End Tags
      2. Whitespace and Case-Sensitivity
      3. Comments
    5. Chapter Summary
  3. 2. Data: Working with Text and Numbers
    1. Text
      1. Defining Text Strings
      2. Manipulating Text
    2. Numbers
      1. Using Different Kinds of Numbers
      2. Arithmetic Operators
    3. Variables
      1. Operating on Variables
      2. Putting Variables Inside Strings
    4. Chapter Summary
    5. Exercises
  4. 3. Logic: Making Decisions and Repeating Yourself
    1. Understanding true and false
    2. Making Decisions
    3. Building Complicated Decisions
    4. Repeating Yourself
    5. Chapter Summary
    6. Exercises
  5. 4. Groups of Data: Working with Arrays
    1. Array Basics
      1. Creating an Array
      2. Choosing a Good Array Name
      3. Creating a Numeric Array
      4. Finding the Size of an Array
    2. Looping Through Arrays
    3. Modifying Arrays
    4. Sorting Arrays
    5. Using Multidimensional Arrays
    6. Chapter Summary
    7. Exercises
  6. 5. Groups of Logic: Functions and Files
    1. Declaring and Calling Functions
    2. Passing Arguments to Functions
    3. Returning Values from Functions
    4. Understanding Variable Scope
    5. Enforcing Rules on Arguments and Return Values
    6. Running Code in Another File
    7. Chapter Summary
    8. Exercises
  7. 6. Data and Logic Together: Working with Objects
    1. Object Basics
    2. Constructors
    3. Indicating a Problem with Exceptions
    4. Extending an Object
    5. Property and Method Visibility
    6. Namespaces
    7. Chapter Summary
    8. Exercises
  8. 7. Exchanging Information with Users: Making Web Forms
    1. Useful Server Variables
    2. Accessing Form Parameters
    3. Form Processing with Functions
    4. Validating Data
      1. Required Elements
      2. Numeric or String Elements
      3. Number Ranges
      4. Email Addresses
      5. <select> Menus
      6. HTML and JavaScript
      7. Beyond Syntax
    5. Displaying Default Values
    6. Putting It All Together
    7. Chapter Summary
    8. Exercises
  9. 8. Remembering Information: Databases
    1. Organizing Data in a Database
    2. Connecting to a Database Program
    3. Creating a Table
    4. Putting Data into the Database
    5. Inserting Form Data Safely
    6. A Complete Data Insertion Form
    7. Retrieving Data from the Database
    8. Changing the Format of Retrieved Rows
    9. Retrieving Form Data Safely
    10. A Complete Data Retrieval Form
    11. Chapter Summary
    12. Exercises
  10. 9. Working with Files
    1. Understanding File Permissions
    2. Reading and Writing Entire Files
      1. Reading a File
      2. Writing a File
    3. Reading and Writing Parts of Files
    4. Working with CSV Files
    5. Inspecting File Permissions
    6. Checking for Errors
    7. Sanitizing Externally Supplied Filenames
    8. Chapter Summary
    9. Exercises
  11. 10. Remembering Users: Cookies and Sessions
    1. Working with Cookies
    2. Activating Sessions
    3. Storing and Retrieving Information
    4. Configuring Sessions
    5. Login and User Identification
    6. Why setcookie() and session_start() Want to Be at the Top of the Page
    7. Chapter Summary
    8. Exercises
  12. 11. Talking to Other Websites and Services
    1. Simple URL Access with File Functions
    2. Comprehensive URL Access with cURL
      1. Retrieving URLs via GET
      2. Retrieving URLs via POST
      3. Using Cookies
      4. Retrieving HTTPS URLs
    3. Serving API Requests
    4. Chapter Summary
    5. Exercises
  13. 12. Debugging
    1. Controlling Where Errors Appear
    2. Fixing Parse Errors
    3. Inspecting Program Data
      1. Adding Debug Output
      2. Using a Debugger
    4. Handling Uncaught Exceptions
    5. Chapter Summary
    6. Exercises
  14. 13. Testing: Ensuring Your Program Does the Right Thing
    1. Installing PHPUnit
    2. Writing a Test
    3. Isolating What You Test
    4. Test-Driven Development
    5. More Information About Testing
    6. Chapter Summary
    7. Exercises
  15. 14. Software Engineering Practices You Should Be Aware Of
    1. Source Control
    2. Issue Tracking
    3. Environments and Deployment
    4. Scaling Eventually
    5. Chapter Summary
  16. 15. Handling Dates and Times
    1. Displaying the Date or Time
    2. Parsing a Date or Time
    3. Calculating Dates and Times
    4. Working with Timezones
    5. Chapter Summary
  17. 16. Package Management
    1. Installing Composer
    2. Adding a Package to Your Program
    3. Finding Packages
    4. Getting More Information on Composer
    5. Chapter Summary
  18. 17. Sending Email
    1. Swift Mailer
    2. Chapter Summary
  19. 18. Frameworks
    1. Laravel
    2. Symfony
    3. Zend Framework
    4. Chapter Summary
  20. 19. Command-Line PHP
    1. Writing Command-Line PHP Programs
    2. Using PHP’s Built-in Web Server
    3. Running a PHP REPL
    4. Chapter Summary
  21. 20. Internationalization and Localization
    1. Manipulating Text
    2. Sorting and Comparing
    3. Localizing Output
    4. Chapter Summary
  22. A. Installing and Configuring the PHP Engine
    1. Using PHP with a Web-Hosting Provider
    2. Installing the PHP Engine
      1. Installing on OS X
      2. Installing on Linux
      3. Installing on Windows
    3. Modifying PHP Configuration Directives
    4. Appendix Summary
  23. B. Answers to Exercises
    1. Chapter 2
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    2. Chapter 3
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    3. Chapter 4
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    4. Chapter 5
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    5. Chapter 6
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    6. Chapter 7
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    7. Chapter 8
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    8. Chapter 9
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    9. Chapter 10
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    10. Chapter 11
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    11. Chapter 12
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    12. Chapter 13
      1. Exercise 2
      2. Exercise 3
      3. Exercise 4
  24. Index