Skip to content

PHP Fundamentals Cheatsheet

<?php echo "Standard tag"; ?>
<?= "Short echo tag" ?>
  • Every statement ends with a semicolon (;)
  • Variables are case-sensitive ($name$Name)
  • Functions/keywords are case-insensitive (echo = ECHO)
// Single-line comment
# Also single-line
/* Multi-line
comment */
/** PHPDoc comment */

$name = "John"; // String
$age = 25; // Integer
$price = 19.99; // Float
$isActive = true; // Boolean
$items = [1, 2, 3]; // Array
  • Must start with $ followed by letter or underscore
  • Can contain letters, numbers, underscores
  • Cannot start with a number or contain hyphens
$global = "accessible everywhere with global keyword";
function example() {
global $global; // Access global variable
$local = "only here"; // Local to function
static $count = 0; // Persists between calls
$count++;
}
$a = 5;
$b = &$a; // $b references $a
$b = 10; // $a is now also 10

// Using define() - can be used anywhere
define('SITE_NAME', 'My Website');
define('COLORS', ['red', 'green', 'blue']); // PHP 7+
// Using const - must be at top level
const MAX_USERS = 100;
const TAX_RATE = 0.08;
if (defined('SITE_NAME')) {
echo SITE_NAME;
}
ConstantDescription
__FILE__Full path of current file
__DIR__Directory of current file
__LINE__Current line number
__FUNCTION__Current function name
__CLASS__Current class name
__METHOD__Current class method

$fruits = ["apple", "banana", "orange"];
echo $fruits[0]; // apple
$fruits[] = "grape"; // Add to end
$user = [
"name" => "John",
"age" => 30,
"email" => "john@example.com"
];
echo $user["name"]; // John
$products = [
"laptop" => ["price" => 800, "qty" => 5],
"mouse" => ["price" => 25, "qty" => 20]
];
echo $products["laptop"]["price"]; // 800
// Indexed
foreach ($fruits as $fruit) {
echo $fruit;
}
// Associative (key => value)
foreach ($user as $key => $value) {
echo "$key: $value";
}
FunctionDescription
count($arr)Get array length
in_array($val, $arr)Check if value exists
array_push($arr, $val)Add to end
array_pop($arr)Remove from end
array_merge($a, $b)Merge arrays
array_keys($arr)Get all keys
array_values($arr)Get all values
sort($arr)Sort array

$name = "PHP";
// Single quotes: literal (no parsing)
echo 'Hello $name'; // Hello $name
// Double quotes: variable interpolation
echo "Hello $name"; // Hello PHP
echo "Hello {$name}!"; // Hello PHP! (explicit syntax)
// Heredoc - parses variables (like double quotes)
$html = <<<HTML
<h1>Welcome, $name!</h1>
<p>This can span multiple lines</p>
HTML;
// Nowdoc - literal (like single quotes)
$js = <<<'JS'
console.log('$name not parsed');
// Also supports multiple lines
JS;
$greeting = "Hello" . " " . "World"; // Hello World
$greeting .= "!"; // Hello World!
FunctionDescription
strlen($str)String length
strtoupper($str)Convert to uppercase
strtolower($str)Convert to lowercase
substr($str, 0, 5)Extract substring
str_replace("a", "b", $str)Replace text
trim($str)Remove whitespace
explode(",", $str)Split string to array
implode(",", $arr)Join array to string

function greet($name) {
return "Hello, $name!";
}
echo greet("John"); // Hello, John!
function greet($name = "Guest", $greeting = "Hello") {
return "$greeting, $name!";
}
greet(); // Hello, Guest!
greet("John"); // Hello, John!
greet("John", "Hi"); // Hi, John!
function increment(&$value) {
$value++;
}
$num = 5;
increment($num);
echo $num; // 6
function sum(...$numbers) {
return array_sum($numbers);
}
echo sum(1, 2, 3, 4); // 10
// Parameter type hints
function greet(string $name): void {
echo "Hello, $name!";
}
// Multiple types with return type
function add(int $a, int $b): int {
return $a + $b;
}
// Nullable types (PHP 7.1+)
function findUser(?int $id): ?array {
return $id ? ['id' => $id] : null;
}
// Union types (PHP 8+)
function process(int|float $value): int|float {
return $value * 2;
}

Common Type Hints: string, int, float, bool, array, object, mixed, void, null

function createUser($name, $email, $age = null) { }
createUser(name: "John", email: "john@test.com");

FunctionChecks for
is_string($var)String
is_int($var)Integer
is_float($var)Float
is_bool($var)Boolean
is_array($var)Array
is_null($var)Null
isset($var)Variable exists and not null
empty($var)Empty value
FunctionDescription
abs($n)Absolute value
round($n)Round number
ceil($n)Round up
floor($n)Round down
max($a, $b)Maximum value
min($a, $b)Minimum value
rand($min, $max)Random number
echo date('Y-m-d'); // 2024-01-15
echo date('H:i:s'); // 14:30:00
echo time(); // Unix timestamp
echo strtotime('+1 week'); // Timestamp in 1 week