PHP: The "Right" Way
Minimum price
Suggested price

PHP: The "Right" Way

Your guide to PHP best practices, coding standards, and authoritative tutorials.

About the Book

There’s a lot of outdated information on the Web that leads new PHP users astray, propagating bad practices and insecure code. PHP: The Right Way is an easy-to-read, quick reference for PHP popular coding standards, links to authoritative tutorials around the Web and what the contributors consider to be best practices at the present time.

There is no canonical way to use PHP. This book aims to introduce new PHP developers to some topics which they may not discover until it is too late, and aims to give seasoned pros some fresh ideas on those topics they’ve been doing for years without ever reconsidering. This ebook will also not tell you which tools to use, but instead offer suggestions for multiple options, when possible explaining the differences in approach and use-case.

This is a living document and will continue to be updated with more helpful information and examples as they become available.

About the Editors

Phil Sturgeon
Phil Sturgeon

Since 2010 I've worked as a freelancer, consultant, Head of API, and CTO, for several API-centric technology startups. Most recently I have been working as an internal API consultant for WeWork, where I used my experience to help educate developers, define standards for API design and architecture, and work on open-source tooling like OpenAPI, JSON Schema, and HTTP. WeWork has 50+ APIs, and trying to get them all playing nicely together was a constant source of learning for me and those involved. Timeouts, circuit breakers, using service mesh instead of just hoping for the best, nd generally focusing on better design, leveraging async whenever possible, and all sorts of other interesting issues.

When I'm not fighting APIs I'm riding bikes. Racing them, crashing them, or rolling around with my mates, I'm either on two wheels, writing about tech, having a cider, or D) all of the above.

Josh Lockhart
Josh Lockhart

Creator of the Slim Framework and PHP The Right Way. Senior developer at New Media Campaigns.

Table of Contents

  • 1. Getting Started
    • 1.1 Use the Current Stable Version (7.0)
    • 1.2 Built-in web server
    • 1.3 Mac Setup
    • 1.4 Windows Setup
  • 2. Code Style Guide
  • 3. Language Highlights
    • 3.1 Programming Paradigms
    • 3.2 Namespaces
    • 3.3 Standard PHP Library
    • 3.4 Command Line Interface
    • 3.5 Xdebug
  • 4. Dependency Management
    • 4.1 Composer and Packagist
    • 4.2 PEAR
  • 5. Coding Practices
    • 5.1 The Basics
    • 5.2 Date and Time
    • 5.3 Design Patterns
    • 5.4 Working with UTF-8
  • 6. Dependency Injection
    • 6.1 Basic Concept
    • 6.2 Complex Problem
    • 6.3 Containers
    • 6.4 Further Reading
  • 7. Databases
    • 7.1 MySQL Extension
    • 7.2 PDO Extension
    • 7.3 Interacting with Databases
    • 7.4 Abstraction Layers
  • 8. Templating
    • 8.1 Benefits
    • 8.2 Plain PHP Templates
    • 8.3 Compiled Templates
    • 8.4 Further Reading
  • 9. Errors and Exceptions
    • 9.1 Errors
    • 9.2 Exceptions
  • 10. Security
    • 10.1 Web Application Security
    • 10.2 Password Hashing
    • 10.3 Data Filtering
    • 10.4 Configuration Files
    • 10.5 Register Globals
    • 10.6 Error Reporting
  • 11. Testing
    • 11.1 Test Driven Development
    • 11.2 Behavior Driven Development
    • 11.3 Complementary Testing Tools
  • 12. Servers and Deployment
    • 12.1 Platform as a Service (PaaS)
    • 12.2 Virtual or Dedicated Servers
    • 12.3 Shared Servers
    • 12.4 Building and Deploying your Application
  • 13. Virtualization
    • 13.1 Vagrant
    • 13.2 Docker
  • 14. Caching
    • 14.1 Opcode Cache
    • 14.2 Object Caching
  • 15. Documenting your Code
    • 15.1 PHPDoc
  • 16. Resources
    • 16.1 From the Source
    • 16.2 People to Follow
    • 16.3 Mentoring
    • 16.4 PHP PaaS Providers
    • 16.5 Frameworks
    • 16.6 Components
    • 16.7 Other Useful Resources
    • 16.8 Video Tutorials
    • 16.9 Books
  • 17. Community
    • 17.1 PHP User Groups
    • 17.2 PHP Conferences
    • 17.3 ElePHPants

Causes Supported

Electronic Frontier Foundation

Defending your civil liberties in a digital world.

Based in San Francisco, EFF is a donor-supported membership organization working to protect fundamental rights regardless of technology.

From the Internet to the iPod, technologies are transforming our society and empowering us as speakers, citizens, creators, and consumers. When our freedoms in the networked world come under attack, the Electronic Frontier Foundation (EFF) is the first line of defense. EFF broke new ground when it was founded in 1990—well before the Internet was on most people's radar—and continues to confront cutting-edge issues defending free speech, privacy, innovation, and consumer rights today. From the beginning, EFF has championed the public interest in every critical battle affecting digital rights.

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms

Do Well. Do Good.

Authors have earned$11,038,020writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF, EPUB and/or MOBI files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub