Introduction
A .php file holds a program written in the PHP language, as plain text.
A web server usually runs it through the PHP interpreter for each request and sends
what it prints to the browser, so the file itself stays on the server. PHP also runs
from the command line.
Old projects may use .php3, .php4 or .php5,
named after the PHP version they were written for, and templates often use
.phtml. Which of these a server treats as PHP depends on how it is
set up.
Structure
A PHP file is plain text with no header or signature. Everything is output as written
until a <?php tag starts code, and a ?> tag returns to
plain output, so a file can mix PHP with HTML. The
File structure page covers the tags,
inline HTML, declare and includes, and the
Lexical structure page covers tokens
and literals.
<h1><?php echo htmlspecialchars($title); ?></h1>
<?php
function total(array $items): int
{
return array_sum($items);
}
echo total([1, 2]) . "\n";
Language reference
The pages below describe the whole language, up to the current PHP 8.5. They follow the PHP manual’s language reference, and link to RFCs where they explain a feature.
- File structure – PHP mode and inline HTML, tags, the newline after the closing tag, short tags, the shebang line, declare directives, include and require, and autoloading
- Lexical structure – tokens, comments, keywords, case rules, numeric literals, strings of every kind and interpolation
- Types – scalar and compound types, type juggling, numeric strings, comparison tables and type declarations
- Variables – scope, global and static variables, references, superglobals and constants
- Expressions – the full operator table, arrays and destructuring, match, nullsafe, spread and the pipe operator
- Statements – control structures and their alternative syntax, goto, declare and eval
- Functions – parameters, named arguments, variadics, closures, arrow functions, first-class callables and generators
- Classes – properties, readonly, hooks, methods, visibility, static members, late static binding, magic methods and cloning
- Interfaces and traits – interfaces, abstract classes, traits and conflict resolution
- Enums – pure and backed enums, methods, restrictions and serialisation
- Attributes – syntax, targets, declaring attribute classes, Reflection and the built-in attributes
- Namespaces – declarations, importing with use, and the name resolution rules
- Exceptions – the Throwable hierarchy, try, catch and finally, error levels and handlers
- php.ini and SAPIs – how a file is run, how web servers choose PHP files, the settings that matter and OPcache
- Built-ins – core and bundled extensions, predefined interfaces and classes, and SPL
- Versions – every release from PHP/FI to 8.5, with what each added and removed
Not described
Known, not yet covered
- Reference: the built-ins page names the extensions and the main function families. It does not describe the functions, classes and methods inside any extension (
array_*,DateTime,PDO, theReflectionclasses, cURL,mbstring); the manual does. Extensions that are not bundled (mysqli,sodium, Xdebug) are not mentioned. - Reference: Composer (
composer.json,composer.lock, Packagist), the PHP-FIG standards other than the PSR-4 autoloading rule, and the tools around PHP files (PHPUnit, static analysers, formatters) are not described. - Viewer:
enumis coloured as a keyword only in front of an enum declaration (a name, an optional: stringor: intbacking type andimplements, then{or the end of the line), soenum_exists()and a variable or parameter named enum stay plain. Thefrominyield fromis plain. Everything after__halt_compiler();(or__halt_compiler() ?>) is data and is left uncoloured, as the compiler stops reading there. - Viewer: a double-quoted string or heredoc is one string colour from end to end: variables and
{$expression}parts inside it are not coloured as code. Class, interface, trait and enum names, namespace names and predefined constants (PHP_EOL,E_ALL) are plain, and calls are coloured as functions by name only. - Viewer: the file is not parsed or run: there is no outline or list of problems, keywords of every version are coloured whatever the PHP version, and only the first 1 MiB is read and coloured.
Links
Software
- PHP – the interpreter that runs PHP files (Windows, macOS, Linux)
- PhpStorm – JetBrains’ development environment for PHP (Windows, macOS, Linux)
- Visual Studio Code – free code editor with PHP support (Windows, macOS, Linux)
- Vim – text editor with PHP syntax support (Windows, macOS, Linux)
Developers
- PHP-Parser – a parser for PHP source (PHP)
- tree-sitter-php – PHP grammar for the Tree-sitter parser used by editors
Reference
- PHP manual – the language and library reference
- PHP language specification – a written description of the language, kept by the PHP project