How to use this topic

The first ten pages cover the language: what PHP is and how to run it, variables and types, operators, strings, arrays, control flow, functions, classes and objects, and errors. The next four are about PHP on the web: requests and forms, sessions and cookies, files and JSON, and databases with PDO. The last four are about working professionally: Composer, security, modern PHP and testing, and deployment.

The examples run for real on the command line and show their exact output; code that only makes sense inside a web request is shown without output. HTTP itself is explained in Web & APIs, SQL in Databases & SQL, the patterns used by preg_match in Regular Expressions, and the attacks behind the security advice in Web Application Security; these pages link there.

Learning path

  1. 01What Is PHP?What PHP is and where it runs: a short history, how a web server hands a request to PHP, the command line, and which PHP versions are supported today.Beginner
  2. 02Running PHP: CLI, the Built-in Server and php.iniRunning PHP while you learn and work: scripts on the command line, the interactive shell, the built-in development server, and the php.ini settings that change how PHP behaves.Beginner
  3. 03Variables, Types and Type JugglingHow PHP stores values: variables with $, the scalar types, null, arrays and objects, type juggling between strings and numbers, var_dump, and strict_types.Beginner
  4. 04Operators and ComparisonArithmetic, string and logical operators, why === is safer than ==, the comparison rules that changed in PHP 8, the spaceship operator, and the null coalescing operator.Beginner
  5. 05StringsWorking with text in PHP: single and double quotes, interpolation, heredoc and nowdoc, common string functions, formatting with sprintf, and multibyte text with mb_ functions.Beginner
  6. 06ArraysPHP's all-purpose array: lists and associative arrays, adding and removing items, looping, sorting, array_map, array_filter and friends, and destructuring.Beginner
  7. 07Control Flow and LoopsMaking decisions and repeating work in PHP: if, elseif and else, switch versus match, while, for and foreach, and break and continue.Beginner
  8. 08FunctionsWriting reusable code in PHP: parameters, default values, type declarations and return types, named arguments, variadics, scope, closures and arrow functions.Beginner
  9. 09Classes and ObjectsObject-oriented PHP: classes, properties and methods, constructors with property promotion, visibility, readonly properties, static members, inheritance, interfaces, traits and enums.Intermediate
  10. 10Errors and ExceptionsWhen things go wrong in PHP: warnings, notices and fatal errors, exceptions with try, catch and finally, the Throwable hierarchy, error_reporting and display_errors, and logging.Beginner
  11. 11PHP and the Web: Requests, Forms and SuperglobalsHow PHP sees an HTTP request: the superglobals, reading form input with GET and POST, validating and escaping it, sending headers and status codes, and redirects.Beginner
  12. 12Sessions and CookiesRemembering users between requests in PHP: setting and reading cookies, how sessions work, session_start, regenerating the session ID, and secure cookie settings.Intermediate
  13. 13Working with Files and JSONReading and writing data in PHP: files with file_get_contents and fopen, paths and directories, CSV, JSON encoding and decoding, and dates with DateTimeImmutable.Beginner
  14. 14Databases with PDOTalking to databases from PHP with PDO: connecting with a DSN, prepared statements with placeholders, fetching rows, transactions, and error handling.Intermediate
  15. 15Composer and PackagesUsing other people's code safely: Composer, composer.json and composer.lock, installing and updating packages, autoloading with PSR-4, and checking dependencies for known vulnerabilities.Intermediate
  16. 16PHP Security BasicsWriting PHP that resists the common attacks: escaping output, prepared statements, password hashing, CSRF tokens, safe file uploads and includes, unserialize, and keeping PHP up to date.Intermediate
  17. 17Modern PHP: Versions, Standards and TestingKeeping PHP code maintainable: what changed in PHP 8, the PHP-FIG standards and coding style, automated tests with PHPUnit, static analysis, and frameworks.Intermediate
  18. 18Deploying PHPRunning PHP in production: web server and PHP-FPM, OPcache, production php.ini settings, logging, file permissions, upgrading PHP versions, and keeping applications patched.Intermediate

PHP glossary

Short definitions of the terms used in this topic. The full glossary covers every topic.