Php Cheat Sheet



PHP is a server scripting language.This server side code can be used to fill out HTML templates in order to create a complete HTML document for a visitor. This finished document is called a dynamic webpage. Dynamic PHP webpages can deliver a custom set of assets to each visitor, unlike static pages which deliver the same set of assets to everyone. PHP Cheat Sheet by Dave Child (DaveChild) via cheatography.com/1/cs/2/ PHP Array Functions arraydiff (arr1, arr2.) arrayfilter (arr, function) arrayflip (arr) arrayintersect (arr1, arr2.) arraymerge (arr1, arr2.) arraypop (arr) arraypush. This PHP cheat sheet collection is inspired by the Blueshoes cheat sheet and licensed under the GPLv3 software license. You can view and fork the source on GitHub. Pull requests welcome.

PHP Variable Comparison

Important notes on the comparisons and tests:

  1. There appear to be two null values at the start of the test. The first one is actually an unset variable, i.e. no longer in existence. The second is a variable which was set to null.
  2. Tests with Ctype and strcmp-type functions use the system default C locale. Results may vary for other locales.
  3. Some tests might seem a bit ‘silly’, for instance testing with 'null'. The reason for adding these tests can be divided into two categories:
    1. Data received from databases and $_POST/$_GET/etc variables are always received as strings (unless a potentially used database abstraction layer changes this). So sometimes testing for a string value where a non-string variable type would be more logical, can actually make sense.
    2. Some are unfortunately regularly encountered in code and added here to illustrate why not to use them.
  4. BEWARE: some variable changing functions will not return a changed value, but will return whether the changing succeeded. You will find this indicated in the function header by the use of $copy (as the test will use a copy of the variable to not influence the other tests in the same table).

Notes on some variables:

Some of the test variables used, do not print the way they are set, either because they contain invisible characters or because they result in something else, so for your convenience, these are outlined here:

How the variable is defined:
i8$x = 0xCC00F9; // Hexadecimal integer.
i9$x = 052; // Octal integer.
ia$x = 0b0111001; // Binary integer (PHP5.4+).
ib$x = ‏௫‏; // Tamil digit five - entered as string as PHP itself cannot deal with it as an integer.
ic$x = ⁸₈; // Unicode superscript and subscript digit 8 - entered as a string as PHP itself cannot deal with these as integers.
f5$x = acos(8); // = NAN
f6$x = NAN; // = Not a number.
f7$x = log(0); // = Infinite.
f8$x = INF; // = Infinite.
f9$x = 1.2345E8; // Exponent notation float.
fa$x = ⅕; // Unicode character representing 1/5 - entered as string as PHP itself cannot deal with it as a float.
sk$x = '123, 'str'rn';
sn$x = 'ftrn';
sp$x = 'x7ftrn'

Legend / How to use the tables:

Php Cheat Sheet
  • The error level for this test sheet has been set to E_ALL & ~E_STRICT. All errors are caught and referenced (with #links) in the tables with details of the error messages (if any) below each table. Similar error messages are grouped together for your convenience.
  • Some column labels have been shortened to avoid them taking undue space. These are indicated by a . If you mouse-over the column label you will see the full variable/test information.
  • In comparison tables, the left-top table header will indicate the comparison used and link to the relevant page in the PHP Manual.
  • In test tables, the left top table header indicates the type of tests. Both this header as well as most column headers link to their respective relevant PHP Manual pages.
  • A ‡ with a number next to a column header means there is a (linked) footnote for that entry at the bottom of the page.
  • When you mouse-over the table the row and column you are at are highlighted. To help you compare specific columns/rows, you can click on any cell to mark the column and row which the cell intersects for extra highlighting. Click again to remove this sticky highlight.

Legend to the color coding

NULL:null
Boolean:true / false
Integer:123456789 / 0
Float:12345.6789
String:‘A string’
Array and Object:Indicated as such. Array keys, array values and object properties will be color coded according to the coding shown here.
Resources:Resource id #15

# Arrays

# Destructuring 7.1

You can destructure arrays to pull out several elements into separate variables:You can skip elements:As well as destructure based on keys:

# Rest and Spread Operators 5.6

Arrays can be spread into functions:Functions can automatically collect the rest of the variables using the same operator:Rest parameters can even be type hinted:
7.4 Arrays with numerical keys can also be spread into a new array:

# Attributes 8.0

Make your own by tagging a class with #[Attribute]

Php Cheat Sheet Pdf

You can add them to properties, (anonymous) classes, functions, constants, closures, function arguments:Use reflection to get them, you can pass in optional arguments to `

# Exceptions 8.0

Throwing an exception is an expression now, which means there are more places you can throw from, such as short closures or as a null coalescing fallback:

You also don't have to catch an exception with a variable anymore:

# Match 8.0

Similar to switch, but with strong type checks, no break keyword, combined arms and it returns a value:

# Dealing with null

# Null Coalescing 7.0

Use the null coalescing operator to provide a fallback when a property is null:

It also works nested:

7.4 You can use the null coalescing assignment operator to write the value into the original variable when it's null:

# Nullsafe Operator 8.0

Chain methods that possibly return null:

The nullsafe operator can also be chained multiple times:

# Named Arguments 8.0

Pass in arguments by name instead of their position:

Named arguments also support array spreading:

# Performance

# The JIT 8.0

Php Cheat Sheet

Php For Dummies

Enable the JIT by specifying a buffer size in your ini settings; you can switch the jit mode between function or tracing:

# Preloading 7.4

Add opcache.preload to your ini settings:

Every file that's loaded in the preload script will be preloaded into memory until server restart.

# Property Promotion 8.0

Prefix constructor arguments with public, protected or private to make them promoted:

You can still add a constructor body, and combine both promoted and non-promoted properties:

Php Cheat Sheet

# Short Closures 7.4

Short closures have automatic access to the outer scope, and only allow a single expression which is automatically returned:

Php Commands

# Types

# Built-in Types

During the PHP 7.x releases and with PHP 8, several new built-in types were added:

  • void: a return type indicating nothing's returned 7.1
  • static: a return type representing the current class or its children 8.0
  • object: anything that is an object 7.2
  • mixed: anything 8.0

Php Cheat Sheet

# Typed properties 7.4

Add types to your class properties:

Beware of the uninitialized state, which is only checked when reading a property, and not when constructing the class:

Php Reference Pdf

Php cheat sheet with examples

# Union Types 8.0

Mysql Cheat Sheet Github

Combine several types into one union, which means that whatever input must match one of the given types: