PHP

PHP Cheatsheet

Core PHP syntax - variables, arrays, and the string/array functions used constantly.

Basics

$x = 5;

Variable assignment

$arr = [1, 2, 3];

Array literal

$assoc = ["key" => "value"];

Associative array

function add($a, $b) { return $a + $b; }

Function declaration

echo "Hello $name";

String interpolation (double quotes)

Arrays

array_map(fn($x) => $x * 2, $arr)

Transform each element

array_filter($arr, fn($x) => $x > 0)

Filter elements

in_array($x, $arr)

Check if a value exists in an array

count($arr)

Number of elements

implode(", ", $arr)

Join array into a string

Common functions

strlen($s)

String length

trim($s)

Remove leading/trailing whitespace

explode(",", $s)

Split a string into an array

isset($x)

Check if a variable is set

$x ?? $default

Null coalescing operator