๐Ÿ“˜Getting Started with PHP
Estimated reading: 3 minutes 65 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงท PHP Cheatsheet

Or Copy Link

CONTENTS
Scroll to Top