PHP Coding Cheat Sheet

PHP Coding Cheat Sheet

PHP is one of the most popular server-side scripting languages for building dynamic and interactive web applications. Its simplicity and flexibility have made it a favourite among both beginners and seasoned developers. This cheat sheet provides a quick reference to essential PHP concepts—from basic syntax and variable handling to advanced topics like object-oriented programming and error management—allowing you to code more efficiently and debug faster. Whether you’re just starting out or looking to polish your skills, this guide offers clear, concise examples and practical tips to help you master PHP.

Designed as a comprehensive, printable resource, this PHP Coding Cheat Sheet is an indispensable tool for web developers. It covers a wide range of topics, including loops, conditional statements, arrays, functions, file inclusion, and database interactions with PDO. With detailed explanations and real-world examples, the cheat sheet simplifies complex concepts and ensures that you have a reliable reference at your fingertips. Download, print, and keep this guide nearby as you build, maintain, and improve your web projects.

ConceptSyntax/ExampleNotes
PHP Opening & Closing Tags <?php
// Your PHP code here
?>
Indicates the start and end of a PHP script
Echo Statementecho "Hello, World!";Outputs text to the browser
Variables$variable = value;Variables start with a $; no type declaration needed
Comments// single-line
/* multi-line */
Adds notes to code
If Statementif (condition) { ... }Conditional logic
For Loopfor ($i = 0; $i < 10; $i++) { ... }Iterates a fixed number of times
While Loopwhile (condition) { ... }Repeats while condition is true
Do-While Loopdo { ... } while (condition);Executes at least once
Foreach Loopforeach ($array as $value) { ... }Iterates over arrays
Switch Statementswitch($var) { case 'a': ... break; default: ... }Multi-branch selection
Ternary Operator$result = (condition) ? value1 : value2;Shorthand for if/else
Function Declarationfunction myFunc($param) { ... }Defines reusable code
Indexed Array$arr = array(1,2,3);Numeric keys automatically assigned
Associative Array$assoc = array("key" => "value");Key-value pairs
Constantsdefine("NAME", "value");Defines a constant value
String Concatenation$str = "Hello" . " World";Uses the dot operator
Type Casting(int)$var, (string)$varConverts variable types
Null Coalescing Operator$var = $a ?? 'default';Returns left operand if set, else default
Superglobals$_GET, $_POST, $_SERVER, $_SESSIONPredefined global arrays
File Inclusioninclude 'file.php';
require 'file.php';
Inserts external PHP files
Class Declarationclass MyClass { ... }Defines an object-oriented class
Interfacesinterface MyInterface { public function myMethod(); }Contracts for classes
Traitstrait MyTrait { ... }Reusable sets of methods
Anonymous Functions$func = function($x) { return $x * 2; };Functions without a name (closures)
Arrow Functions$fn = fn($x) => $x + 1;Shorter syntax for closures (PHP 7.4+)
Error Handlingtry { ... } catch (Exception $e) { ... }Manages exceptions
Sessionssession_start(); $_SESSION['user'] = 'Alice';Maintains user state
Cookiessetcookie("name", "value", time()+3600);Stores data on the client
Regular Expressionspreg_match('/pattern/', $string, $matches);Pattern matching in strings
PDO (PHP Data Objects)$pdo = new PDO($dsn, $user, $pass);Database access abstraction layer
JSON Functionsjson_encode($data); json_decode($json);Convert between JSON and PHP arrays/objects
Date & Time Functionsdate("Y-m-d"); time();Formats dates and gets the current timestamp