PHP Reference
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. The reference material consulted while preparing these pages is listed in the section bibliography. |
Welcome to the PHP reference. PHP is a general-purpose scripting language best known for the
web: it runs a large share of the world’s websites, from WordPress and Drupal to applications built with Laravel and
Symfony, and it is just as usable for command-line tools, workers and services. Created by Rasmus Lerdorf in 1994,
it is developed in the open: the language changes through public RFC votes, and the
PHP Foundation funds core developers. Modern PHP is a very different language from the
PHP of older books and tutorials: since 7.0 it has gained a fast engine, scalar and return types, match, enums,
readonly properties and classes, fibers, attributes, property hooks, asymmetric visibility and the pipe operator.
This section documents PHP 8.5 (latest patch 8.5.11, 2026-09-24), checked on 2026-10-05. Each feature added in a recent version is labelled with that version, so readers on 8.3 or 8.4 can see what they have. PHP 8.6 is upcoming (release candidate on that date, general availability scheduled for 2026-11-19); its additions appear only where marked "upcoming in 8.6". Each PHP release is supported for four years: two years of active support with bug and security fixes, then two years of security fixes only, both ending on 31 December. On 2026-10-05, 8.4 and 8.5 are in active support, 8.2 and 8.3 receive security fixes only, and 8.1 and older are end of life (see What’s New and Migration for the full timeline).
The examples share one small, original domain: a library catalogue of books, authors and loans, run from the command line. Every example on these pages was run with PHP 8.5.11 (upcoming-8.6 examples with 8.6.0RC2), and the tool output shown comes from the versions named on each page.
New here? Read in this order
-
Getting Started — install PHP and run a first script.
-
Basic Syntax and Style, then the Types and values group, from Types and the Type System to Control Structures.
-
Classes and Objects and the rest of Object-oriented programming, in order.
-
Composer and Packages and Testing with PHPUnit and Pest, the two tools every PHP project uses.
The other pages can be read in any order as you need them. Coming from an older book or tutorial? Start with What’s New and Migration; every page also ends with a section on where older material is outdated.
What’s covered
Getting started
-
Getting Started — what PHP is and can do, a short history from PHP/FI to 8.5, the PHP Foundation and the RFC process, installing PHP on Linux, macOS, Windows and Docker, the CLI, the built-in web server, and how a script runs from source to opcodes.
-
Configuration and Extensions —
php.iniand its search order,.user.ini,ini_set()and the changeable modes, the core directives, loading extensions, PECL versus PIE,phpinfo()and the SAPIs. -
Basic Syntax and Style — tags, escaping from HTML, statements and comments, reserved words, naming, PSR-1 and PER Coding Style 3, and PHPDoc.
Types and values
-
Types and the Type System — every type, union, intersection and DNF types, nullable types,
neverandvoid, andstrict_types. -
Type Juggling and Comparisons — conversion rules and casts, the PHP 8 string-to-number comparison change,
==versus===, and the comparison tables. -
Variables, Scope and Constants — assignment, scope,
globalandstatic, superglobals,isset()/empty()/unset(), constants and magic constants. -
Operators and Expressions — every operator with precedence, including
??,??=,<=>,?->, the pipe operator|>(8.5) and the spread operator. -
Control Structures —
if, loops,foreachby reference,switchversusmatch,declare, andinclude/require.
Strings, arrays and functions
-
Strings — quoting, interpolation, heredoc and nowdoc, and the string API by family.
-
Unicode, mbstring and intl — bytes, code points and graphemes, mbstring, and locale-aware formatting with intl.
-
Regular Expressions — PCRE2 syntax and modifiers, the
preg_*functions, Unicode matching and ReDoS. -
Arrays — the ordered-map model, key casting, destructuring, and the array API including
array_find()(8.4) andarray_first()(8.5). -
Functions — arguments by value and by reference, defaults, variadics, named arguments, return types and recursion.
-
Closures and Callables — closures and
use, arrow functions, theClosureclass, first-class callable syntax and every callable form.
Object-oriented programming
-
Classes and Objects — properties, constants, constructors and promotion, static members, anonymous classes and object comparison.
-
Visibility, Readonly and Asymmetric Visibility —
public/protected/private, readonly properties and classes, andpublic private(set)(8.4). -
Property Hooks —
getandsethooks (8.4), backed and virtual properties, and hooks in interfaces. -
Inheritance, Interfaces and Abstract Classes —
extends, overriding and signature rules,final, abstract classes, interfaces,#[\Override]and variance. -
Traits — using traits, conflict resolution, abstract and static members, and when to prefer composition.
-
Enumerations — pure and backed enums (8.1), methods, interfaces, constants and serialisation.
-
Magic Methods and Overloading — every magic method, property and method overloading, and the serialisation hooks.
-
Cloning, Late Static Binding and Object Lifecycle —
cloneandclone()with properties (8.5),static::, weak references and lazy objects (8.4). -
Namespaces and Autoloading — namespaces,
useand name resolution,spl_autoload_register()and PSR-4. -
Attributes — syntax, custom attribute classes, reading them with Reflection, and every predefined attribute.
-
Reflection — the Reflection API for classes, methods, properties, types, enums and attributes.
Errors, iteration and concurrency
-
Errors and Exceptions — the
Throwablehierarchy,try/catch/finally, custom exceptions, error levels and handlers. -
Iterators and Generators — the iterator interfaces, the SPL iterators, and generators with keys,
send()andyield from. -
Fibers and Asynchronous PHP — fibers (8.1), a minimal scheduler, the event-loop libraries built on them, and process-level concurrency.
-
References and Memory Management — references, zvals and copy-on-write, and the cycle garbage collector.
Standard library
-
SPL Data Structures — stacks, queues, heaps, fixed arrays, object storage and when a plain array is better.
-
Date and Time —
DateTimeImmutable, parsing and formatting, intervals and periods, time zones, and testable clocks. -
Math, Numbers and Randomness — the maths functions, floating-point pitfalls, BCMath and GMP, and the
Randomextension. -
Files, Streams and I/O — reading and writing files, CSV, locking, directories, stream wrappers, filters and contexts.
-
JSON, XML and Serialization — JSON with exceptions,
serialize()safety, SimpleXML, DOM and the HTML5 parser, XMLReader/XMLWriter and XSL. -
Cryptography, Hashing and Passwords — hashing, HMAC,
password_hash(), sodium, OpenSSL and secure randomness. -
Command-Line PHP — the CLI SAPI, arguments and options, streams and exit codes, running programs safely, signals, and Symfony Console.
Tooling, quality and evolution
-
Composer and Packages —
composer.json, version constraints, the lock file, autoloading, scripts, auditing and PIE. -
PSR Standards and Coding Style — every accepted PSR, PER Coding Style 3, PHP_CodeSniffer, PHP-CS-Fixer and Pint.
-
Static Analysis and Refactoring — PHPStan, PHPDoc types and generics, Psalm, and Rector.
-
Testing with PHPUnit and Pest — PHPUnit 13 and Pest 5, test doubles, coverage, architecture tests and mutation testing.
-
Debugging and Profiling — dumping values, backtraces, Xdebug 3, phpdbg and system tracing probes.
-
OPcache, JIT and Performance — OPcache settings, preloading, the JIT with measurements, FFI and benchmarking.
-
What’s New and Migration — every release from 7.0 to 8.6, the support timeline, deprecations, PHP 9, upgrading, and what older books get wrong.
-
Cheat Sheet — a one-page summary of the language, with a downloadable PDF.
PHP for the web
This section covers the language: syntax, types, objects, the standard library and the tools around them. How PHP serves HTTP requests (the request model, PHP-FPM and web servers, superglobals, forms, sessions, uploads, PDO and web security) and how to build applications with the Laravel framework are covered in the PHP and Laravel section under Web Development, which builds on the pages here and links back to them. Start with The PHP Request Model and Servers for plain PHP on the web, or Laravel Getting Started for the framework; the PHP and Laravel cheat sheet summarises both.
Bibliography
The reference material consulted while preparing this section. The PHP Manual and the official documentation of each tool are the primary sources; the books are consulted references, not the primary source. On any discrepancy, or wherever a book is silent on a later feature, the official documentation wins. No text, code listing or figure is copied from any book.
Books
-
Andrew Beak, PHP 7 Zend Certification Study Guide: Ace the ZCE 2017-PHP Exam, 1st edition, Apress, 2017. ISBN 978-1-4842-3245-3 (print), 978-1-4842-3246-0 (eBook), DOI 10.1007/978-1-4842-3246-0. Targets PHP 7.1.
-
David Powers, PHP 7 Solutions: Dynamic Web Design Made Easy, 4th edition, Apress, 2019. ISBN 978-1-4842-4337-4 (print), 978-1-4842-4338-1 (eBook), DOI 10.1007/978-1-4842-4338-1. Targets PHP 7.2/7.3. Companion code: Apress/php-7-solutions. Current edition: PHP 8 Solutions, 5th edition (ISBN 978-1-4842-7140-7), companion code Apress/php-8-solutions.
-
Frank M. Kromann, Beginning PHP and MySQL: From Novice to Professional, 5th edition, Apress, 2018. ISBN 978-1-4302-6043-1 (print), 978-1-4302-6044-8 (eBook), DOI 10.1007/978-1-4302-6044-8. Targets PHP 7.0-7.2. Companion code: Apress/beginning-php-and-mysql-5e.
-
Robin Nixon, Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5, 5th edition, O’Reilly Media, 2018. ISBN 978-1-491-97891-7. Targets PHP 5.4-7.1; only its PHP chapters were consulted. Companion code (6th edition): RobinNixon/lpmj6. Current edition: the 7th edition (2025).
The PHP Manual (php.net/manual), as the manual is organised:
-
Getting Started: Introduction and A simple tutorial; History of PHP.
-
Installation and Configuration: Installation, PIE, PECL and Runtime configuration with the php.ini directive list and core directives.
-
Language Reference: basic syntax, types, variables, constants, expressions, operators, control structures, functions, classes and objects, namespaces, enumerations, errors, exceptions, fibers, generators, attributes and references, plus the predefined variables, exceptions, interfaces and attributes.
-
Features: command line usage, garbage collection and DTrace dynamic tracing.
-
Function Reference, the extensions used individually: Arrays, BCMath, Date/Time, Directories, DOM, Error Handling, Program Execution, FFI, Fileinfo, Filesystem, GMP, Hash, iconv, intl, JSON, Math, mbstring, OPcache, OpenSSL, Password Hashing, PCNTL, PCRE, phpdbg, POSIX, Random, Reflection, SimpleXML, Sodium, SPL, Streams, Strings, XMLReader, XMLWriter and XSL.
-
FAQ: Safe password hashing.
-
Appendices, including the migration guides from 7.0 to 8.5 and the reserved words.
php.net and the PHP project
-
Supported Versions, Unsupported Branches, Releases, the PHP 8 ChangeLog and the PHP 8.5 release page.
-
The PHP RFC wiki (the RFCs cited on individual pages) and the PHP 8.6 release plan.
-
php-src, including the UPGRADING notes for 8.6.
Standards
Tooling documentation
-
PHP_CodeSniffer, PHP-CS-Fixer, PHPCompatibility and Laravel Pint.
-
phpDocumentor (PHPDoc).
-
PHP The Right Way, as a community reference.