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

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 website 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.

Josh Lockhart

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

Phil Sturgeon

Phil has spent years developing websites with a bunch of languages and frameworks such as PHP, Laravel, Ruby on Rails, Python and EmberJS to name but a few. Over the years he has ended up writing handfuls of APIs and worked implementing even more. This gave him considerable perspective on the good, the bad and the ugly of API development.

Being a core-contributor to CodeIgniter, FuelPHP and PyroCMS has given him some insight on framework-interoperability - leading him to join the PHP-FIG, which strives to make lives easier for developers through standards and interfaces.

Now he uses this extensive and random experience to try to improve the quality of code in the PHP community, one package, book, framework, or API at a time.

  • 1 Getting Started
    • 1.1 Use the Current Stable Version (5.5)
    • 1.2 Built-in web server
    • 1.3 Mac Setup
    • 1.4 Windows Setup
    • 1.5 Vagrant
  • 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
  • 6 Dependency Injection
    • 6.1 Basic Concept
    • 6.2 Complex Problem
    • 6.3 Containers
    • 6.4 Further Reading
  • 7 Databases
    • 7.1 PDO
    • 7.2 Abstraction Layers
  • 8 Errors and Exceptions
    • 8.1 Errors
    • 8.2 Exceptions
  • 9 Security
    • 9.1 Web Application Security
    • 9.2 Password Hashing
    • 9.3 Data Filtering
    • 9.4 Configuration Files
    • 9.5 Register Globals
    • 9.6 Error Reporting
  • 10 Testing
    • 10.1 Test Driven Development
    • 10.2 Behavior Driven Development
    • 10.3 Complementary Testing Tools
  • 11 Servers and Deployment
    • 11.1 Platform as a Service (PaaS)
    • 11.2 Virtual or Dedicated Servers
    • 11.3 Shared Servers
    • 11.4 Building and Deploying your Application
  • 12 Caching
    • 12.1 Bytecode Cache
    • 12.2 Object Caching
  • 13 Resources
    • 13.1 From the Source
    • 13.2 People to Follow
    • 13.3 Mentoring
    • 13.4 PHP PaaS Providers
    • 13.5 Frameworks
    • 13.6 Components
  • 14 Community
    • 14.1 PHP User Groups
    • 14.2 PHP Conferences