PHP Cheat Sheet
PHP syntax, associative arrays, superglobals, and object-oriented features for server-side web development and dynamic scripting.
2 PagesBeginnerMar 22, 2026
Basic Syntax
Variables, control flow, and output.
php
<?php$age = 30;$name = "Ada";$pi = 3.14159;if ($age >= 18) { echo "$name is an adult\n";}for ($i = 0; $i < 5; $i++) { echo "Count: $i\n";}?>
Arrays
Indexed and associative arrays.
php
<?php$nums = [5, 3, 1, 4, 2];sort($nums); // [1, 2, 3, 4, 5]$ages = ["Alice" => 30, "Bob" => 25]; // associative array$ages["Carol"] = 28;echo $ages["Alice"]; // 30$doubled = array_map(fn($n) => $n * 2, $nums);$evens = array_filter($nums, fn($n) => $n % 2 === 0);?>
Object-Oriented PHP
Classes, constructors, and visibility.
php
<?phpclass Person { public string $name; private int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } public function greet(): string { return "Hello, {$this->name}"; }}$p = new Person("Ada", 30);echo $p->greet();?>
Superglobals
Built-in arrays always available in scope.
- $_GET- associative array of URL query string parameters
- $_POST- associative array of form data sent via POST
- $_SESSION- persists data across requests for a logged-in user
- $_SERVER- server and execution environment information (e.g. REQUEST_METHOD)
- $_ENV- environment variables available to the script
- $_FILES- info about files uploaded via HTTP POST
Pro Tip
Always use prepared statements (PDO with bound parameters) instead of concatenating user input into SQL strings, to prevent SQL injection.
Was this cheat sheet helpful?
Explore Topics
#PHP#PHPCheatSheet#Programming#Beginner#BasicSyntax#Arrays#ObjectOrientedPHP#Superglobals#OOP#DataStructures#WebDevelopment#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance