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

  1. Getting Started — install PHP and run a first script.

  2. Basic Syntax and Style, then the Types and values group, from Types and the Type System to Control Structures.

  3. Strings, Arrays, Functions and Closures and Callables.

  4. Classes and Objects and the rest of Object-oriented programming, in order.

  5. Errors and Exceptions and Namespaces and Autoloading.

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

mindmap root((PHP Reference)) Getting started Getting Started Configuration and Extensions Basic Syntax and Style Types and values Type System Juggling and Comparisons Variables and Constants Operators Control Structures Strings, arrays and functions Strings Unicode, mbstring and intl Regular Expressions Arrays Functions Closures and Callables Object-oriented PHP Classes and Objects Visibility and Readonly Property Hooks Inheritance and Interfaces Traits Enumerations Magic Methods Cloning and Lifecycle Namespaces and Autoloading Attributes Reflection Errors, iteration, concurrency Errors and Exceptions Iterators and Generators Fibers References and Memory Standard library SPL Data Structures Date and Time Math and Randomness Files and Streams JSON, XML, Serialization Cryptography and Passwords Command-Line PHP Tooling and evolution Composer PSR Standards Static Analysis PHPUnit and Pest Debugging and Profiling OPcache and JIT What's New and Migration

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.ini and 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, never and void, and strict_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, global and static, 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, foreach by reference, switch versus match, declare, and include/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) and array_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, the Closure class, 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, and public private(set) (8.4).

  • Property Hooks — get and set hooks (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 — clone and clone() with properties (8.5), static::, weak references and lazy objects (8.4).

  • Namespaces and Autoloading — namespaces, use and 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

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 Random extension.

  • 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

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

The PHP Manual (php.net/manual), as the manual is organised:

php.net and the PHP project

Standards

Tooling documentation