📘Getting Started with PHP
Estimated reading: 3 minutes 374 views

PHP Cheatsheet – The Ultimate Guide (2025 Edition)

Quick Access to PHP Syntax, Functions, and Common Use Cases


Introduction – Why Use a PHP Cheatsheet?

PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language especially suited for web development. Whether you’re building simple websites or complex content management systems like WordPress, having a PHP cheatsheet helps you:

  • Save time with quick syntax reminders
  • Boost productivity with ready-to-use code
  • Avoid common mistakes in functions and structures

PHP Basics

PHP Syntax

<?php
echo "Hello, World!";
?>

PHP Tags

  • <?php ... ?> – Standard opening/closing tag
  • <?= ... ?> – Shortcut for echo

Variables

$name = "John";
$age = 25;

Constants

define("SITE_NAME", "Tech369HUB");

Control Structures

If, Else, Elseif

if ($age > 18) {
    echo "Adult";
} elseif ($age == 18) {
    echo "Just became an adult";
} else {
    echo "Minor";
}

Switch

switch ($day) {
    case "Monday":
        echo "Start of week";
        break;
    default:
        echo "Another day";
}

Loops

// For Loop
for ($i = 0; $i < 5; $i++) {
    echo $i;
}

// While Loop
while ($i < 5) {
    echo $i++;
}

Operators

Arithmetic

$sum = $a + $b;

Comparison

$a == $b
$a === $b
$a != $b

Logical

$a && $b
$a || $b
!$a

Functions

Define a Function

function greet($name) {
    return "Hello, $name!";
}

Built-in Functions

strlen("PHP"); // 3
strtoupper("php"); // PHP

Arrays

Indexed Arrays

$colors = ["red", "green", "blue"];

Associative Arrays

$person = ["name" => "John", "age" => 30];

Array Functions

count($colors);
array_push($colors, "yellow");
array_merge($arr1, $arr2);

Strings

Concatenation

$fullName = $firstName . " " . $lastName;

String Functions

strlen($str);
str_replace("old", "new", $str);
substr($str, 0, 5);

Forms & $_GET/$_POST

$name = $_POST['name'];
$search = $_GET['search'];

File Handling

$file = fopen("file.txt", "r");
$content = fread($file, filesize("file.txt"));
fclose($file);

Sessions & Cookies

Session

session_start();
$_SESSION["user"] = "John";

Cookie

setcookie("user", "John", time() + 3600);

PHP with MySQL

Connect to MySQL

$conn = mysqli_connect("localhost", "user", "pass", "database");

Run a Query

$result = mysqli_query($conn, "SELECT * FROM users");

Fetch Data

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['username'];
}

Error Handling

try {
    // code
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage();
}

PHP Superglobals

SuperglobalDescription
$_GETData from URL parameters
$_POSTData from forms
$_SESSIONSession data
$_COOKIECookies
$_SERVERServer info
$_FILESFile upload data

Common PHP Commands

TaskCommand
Print/Outputecho, print
Terminate Scriptexit(), die()
Check Variable Setisset($var)
Check Variable Emptyempty($var)
Include Fileinclude('file.php')
Require Filerequire('file.php')

Tips & Best Practices

  • Always sanitize user inputs (htmlspecialchars, filter_input)
  • Use prepared statements for SQL queries
  • Prefer === over == for strict comparison
  • Keep error reporting on in dev: error_reporting(E_ALL);
  • Use Composer for managing dependencies

Useful PHP Resources


Final Thoughts

A PHP cheatsheet is essential for fast development and avoiding repetitive Google searches. Bookmark this guide and refer back whenever you’re coding in PHP — it’ll keep your development efficient, accurate, and enjoyable.


Share Now :
Share

🧷 PHP Cheatsheet

Or Copy Link

CONTENTS
Scroll to Top