PHP cheat sheet, PHP syntax guide, PHP examples, PHP reference for beginners, PHP programming basics, PHP functions list, PHP form handling, PHP OOP examples, PHP arrays and loops, PHP interview prep
PHP cheat sheet, PHP syntax guide, PHP examples, PHP reference for beginners, PHP programming basics, PHP functions list, PHP form handling, PHP OOP examples, PHP arrays and loops, PHP interview prep

PHP Cheat Sheet — Complete Syntax Reference and Programming Examples for Beginners

PHP Cheat Sheet — Complete Syntax Reference & Examples

This PHP Cheat Sheet serves as a quick, structured reference for developers who want to learn, revise, or practice PHP efficiently. Whether you’re creating dynamic web applications or backend APIs, this guide covers all essential PHP syntax, operators, data types, functions, and practical examples.


Introduction to PHP

PHP (Hypertext Preprocessor) is a server-side scripting language used to build dynamic websites and web applications. It can be embedded within HTML and is one of the core technologies of full-stack web development.

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

Output:

Hello, PHP World!

PHP Syntax and Variables

PHP Script Structure

PHP scripts start with <?php and end with ?>. Statements must end with a semicolon ;.

Variable Declaration

Variables in PHP start with a $ sign.

<?php
$name = "John";
$age = 25;
$salary = 45000.50;
echo "Employee: $name, Age: $age, Salary: $salary";
?>

PHP Data Types and Type Conversion

Data TypeExampleDescription
String$x = "Hello";Sequence of characters
Integer$x = 100;Whole number
Float$x = 10.5;Decimal number
Boolean$x = true;True or False
Array$x = array(1, 2, 3);Collection of values
Object$x = new ClassName();Instance of class
NULL$x = null;Empty value
ResourceDatabase or file handlerExternal reference

Type Casting Example:

$val = "20";
$intVal = (int)$val;
echo $intVal + 10; // Output: 30

PHP Operators Reference

Operator TypeExampleDescription
Arithmetic+ - * / % **Basic math operations
Assignment= += -= *= /=Assign and modify values
Comparison== === != <> > < >= <=Compare values
Logical`&&
String. .=Concatenate strings
Array+ == === !=Compare or merge arrays

Example:

$a = 10;
$b = 5;
echo $a + $b; // 15

PHP Conditional Statements

<?php
$marks = 85;

if ($marks >= 90) {
    echo "Excellent!";
} elseif ($marks >= 75) {
    echo "Good!";
} else {
    echo "Needs improvement.";
}
?>

PHP Loops and Iterations

While Loop

$i = 1;
while ($i <= 5) {
    echo "Count: $i<br>";
    $i++;
}

For Loop

for ($x = 1; $x <= 3; $x++) {
    echo "Iteration: $x<br>";
}

Foreach Loop

$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
    echo "$color<br>";
}

PHP Functions and Parameters

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

echo greet("Alice");

Output:

Hello, Alice!

Returning Multiple Values

function getValues() {
    return array("PHP", "Python", "Java");
}
list($a, $b, $c) = getValues();
echo "$a, $b, $c";

PHP Arrays — Indexed, Associative, Multidimensional

// Indexed Array
$fruits = array("Apple", "Banana", "Mango");

// Associative Array
$age = array("John" => 25, "Jane" => 22);

// Multidimensional Array
$students = array(
    array("John", 80),
    array("Mary", 90)
);

echo $students[1][0]; // Mary

PHP Strings — Common Functions

FunctionUsageExample
strlen()String lengthstrlen("Hello")
str_replace()Replace textstr_replace("World", "PHP", $text)
strpos()Find substringstrpos($text, "PHP")
strtolower() / strtoupper()Case conversionstrtoupper("php")
trim()Remove whitespacetrim(" PHP ")

PHP Include and Require

include("header.php");
require("config.php");

include: continues script if file missing
require: stops script if file missing


PHP Superglobals Quick Reference

VariableDescription
$_GETCollects data via URL parameters
$_POSTCollects form data
$_SESSIONStores session variables
$_COOKIEStores cookies
$_FILESHandles file uploads
$_SERVERServer information
$_REQUESTCombines GET and POST

PHP Form Handling Example

<form method="post" action="welcome.php">
    Name: <input type="text" name="name">
    <input type="submit">
</form>
// welcome.php
$name = $_POST['name'];
echo "Welcome, $name!";

PHP File Handling

$file = fopen("data.txt", "w");
fwrite($file, "PHP Cheat Sheet Content");
fclose($file);

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

PHP Classes and Object-Oriented Programming (OOP)

class Car {
    public $brand;

    function __construct($brand) {
        $this->brand = $brand;
    }

    function message() {
        return "This car is a $this->brand.";
    }
}

$car1 = new Car("Toyota");
echo $car1->message();

PHP Cheat Sheet Quick Reference Table

ConceptSyntaxExample
Outputecho, printecho "Hi";
Variable$var_name$a = 10;
Arrayarray()$x = array(1,2,3);
Loopfor(), while()for($i=0;$i<5;$i++)
Conditionif(), elseif($a>$b)
Functionfunction name(){}function test(){}
Includeinclude('file.php')include("head.php");
Classclass Name{}class Car{}
PHP cheat sheet, PHP syntax guide, PHP examples, PHP reference for beginners, PHP programming basics, PHP functions list, PHP form handling, PHP OOP examples, PHP arrays and loops, PHP interview prep

Related Internal Resources


Frequently Asked Questions (FAQ)

Q1. What is PHP used for?
PHP is mainly used for creating dynamic web pages, APIs, and backend logic in web development.

Q2. How to run a PHP script?
Save the file as script.php and run it on a local server (XAMPP, WAMP, or PHP CLI).

Q3. What is the difference between include and require?
include shows a warning if the file is missing, while require stops the script completely.

Q4. How to connect PHP with MySQL?
Use the mysqli_connect() function to establish a connection to the database.

Q5. Is PHP still relevant in 2025?
Absolutely. PHP powers over 75% of websites globally, including WordPress and major CMS platforms.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments