Register - Login

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.

Concept Syntax/Example Notes
PHP Opening & Closing Tags <?php
// Your PHP code here
?>
Indicates the start and end of a PHP script
Echo Statement echo "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 Statement if (condition) { ... } Conditional logic
For Loop for ($i = 0; $i < 10; $i++) { ... } Iterates a fixed number of times
While Loop while (condition) { ... } Repeats while condition is true
Do-While Loop do { ... } while (condition); Executes at least once
Foreach Loop foreach ($array as $value) { ... } Iterates over arrays
Switch Statement switch($var) { case 'a': ... break; default: ... } Multi-branch selection
Ternary Operator $result = (condition) ? value1 : value2; Shorthand for if/else
Function Declaration function 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
Constants define("NAME", "value"); Defines a constant value
String Concatenation $str = "Hello" . " World"; Uses the dot operator
Type Casting (int)$var, (string)$var Converts variable types
Null Coalescing Operator $var = $a ?? 'default'; Returns left operand if set, else default
Superglobals $_GET, $_POST, $_SERVER, $_SESSION Predefined global arrays
File Inclusion include 'file.php';
require 'file.php';
Inserts external PHP files
Class Declaration class MyClass { ... } Defines an object-oriented class
Interfaces interface MyInterface { public function myMethod(); } Contracts for classes
Traits trait 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 Handling try { ... } catch (Exception $e) { ... } Manages exceptions
Sessions session_start(); $_SESSION['user'] = 'Alice'; Maintains user state
Cookies setcookie("name", "value", time()+3600); Stores data on the client
Regular Expressions preg_match('/pattern/', $string, $matches); Pattern matching in strings
PDO (PHP Data Objects) $pdo = new PDO($dsn, $user, $pass); Database access abstraction layer
JSON Functions json_encode($data); json_decode($json); Convert between JSON and PHP arrays/objects
Date & Time Functions date("Y-m-d"); time(); Formats dates and gets the current timestamp