PHP Cheat Sheet
Transform, format, and optimize your data instantly with this free client-side tool.
Basics
PHP tags
Output a string
Output a string (returns 1)
Variable declaration
Define a constant
Determine if a variable is declared and is different than null
Determine whether a variable is empty
Unset a given variable
Control Structures
If statement
Else if statement
For loop
Foreach loop (values only)
Foreach loop (keys and values)
While loop
Switch statement
Match expression (PHP 8.0+)
Strings
Get string length
Find the position of the first occurrence of a substring
Replace all occurrences of the search string with the replacement
Make a string lowercase
Make a string uppercase
Return part of a string
Split a string by string
Join array elements with a string
Strip whitespace from the beginning and end of a string
Arrays
Array declaration (short syntax)
Associative array declaration
Count all elements in an array
Push one or more elements onto the end of array
Pop the element off the end of array
Shift an element off the beginning of array
Prepend one or more elements to the beginning of an array
Checks if the given key or index exists in the array
Checks if a value exists in an array
Merge one or more arrays
Sort array in ascending order
Applies the callback to the elements of the given arrays
Filters elements of an array using a callback function
Functions
Function definition
Function with default arguments
Function with type hinting and return type
Arrow function definition
Returns an array comprising a function's argument list
Object Oriented
Class definition
Public property declaration
Class constructor magic method
Instantiate a class
Access object property
Access static property from within the class
Call parent class constructor
Interface definition
Trait definition
Class inheritance and interface implementation
Superglobals
Variables passed to the current script via the URL parameters
Variables passed to the current script via the HTTP POST method
IP address of the client
Session variables
HTTP Cookies
Start new or resume existing session
Send a cookie
File Handling
Opens file or URL
Binary-safe file read
Binary-safe file write
Closes an open file pointer
Reads entire file into a string
Write data to a file
Checks whether a file or directory exists
Error Handling
Start try block
Catch generic exception
Block always executed after try/except
Throw an exception manually
Sets which PHP errors are reported
Includes
Include and evaluate the specified file
Include file, fatal error if not found
Include file only once
Require file only once
Advanced
Arrow function mapping over an array
Enable strict error reporting for debugging
Set PDO to throw exceptions on SQL errors
🔥 Similar Tools
Explore LibraryFrequently Asked Questions
Got questions? We've got answers.