Language cheatsheet
PHP 8 Cheatsheet
The php command line and the modern PHP 8 syntax you use every day: types, enums, match, PDO and more.
Command line
- Check the PHP version — Print the version and build details.
- Start a development server — Serve the current folder at http://localhost:8000. For development only.
- Serve a public folder with a router script — Serve a document root, sending unknown paths to a front controller.
- Run a script — Run a PHP file from the command line.
- Run a one-line command — Run PHP code without a file.
- Check a file for syntax errors — Check syntax without running the file.
- List loaded extensions — Show compiled-in extensions such as pdo_mysql and mbstring.
- Find the php.ini file — Show which configuration files are loaded.
- Read a configuration value — Print the value of a single configuration setting.
- Open the interactive shell — Start an interactive prompt. Exit with Ctrl+D.
Types and functions
- Turn on strict typing — Put this on the first line of a file so scalar type declarations aren't silently converted.
- Declare types — Type parameters and return values. ?string means string or null.
- Use union types — Accept more than one type for a parameter.
- Call with named arguments — Name arguments to skip optional ones and make calls readable.
- Write an arrow function — A short closure that captures outer variables automatically.
- Declare properties in the constructor — Define and assign properties from constructor parameters.
- Define an enum — A fixed set of named values, optionally backed by strings or integers.
- Use match instead of switch — Match with strict comparison. It returns a value and throws if nothing matches.
- Call methods on possible nulls — Short-circuit to null instead of an error.
- Provide a default — Use a default when a value is null or missing.
Strings and arrays
- Search in strings — Check the start, end or contents of a string.
- Insert variables in strings — Use double quotes or curly braces for expressions.
- Transform and filter arrays — Map and filter, then reindex the result.
- Extract a column — Pull one field from each row.
- Sort with a comparison — Sort an array by a field using the spaceship operator.
- Merge arrays — Combine arrays with the spread operator.
- Check keys and values — Test whether a key or value exists.
- Encode and decode JSON — Convert to and from JSON, throwing exceptions on errors.
Web, security and database
- Connect with PDO — Connect to MySQL with exceptions on errors. Use real credentials from the environment.
- Run a prepared statement — Pass values separately from the SQL, which prevents SQL injection.
- Hash a password — Hash passwords with a strong algorithm and a built-in salt. Never store plain text.
- Check a password — Compare a password with a stored hash.
- Escape output — Escape user-provided values before printing them in HTML, to prevent cross-site scripting.
- Validate input — Check that a value is a valid email address.
- Start a session — Call it before any output.
- Redirect — Send a redirect and stop the script.
- Handle exceptions — Catch specific exceptions and always run cleanup in finally.
- Use namespaces and autoloading — Declare a namespace, then load classes through Composer's autoloader.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.