Table of Contents:
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 Type | Example | Description |
|---|---|---|
| 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 |
| Resource | Database or file handler | External reference |
Type Casting Example:
$val = "20";
$intVal = (int)$val;
echo $intVal + 10; // Output: 30
PHP Operators Reference
| Operator Type | Example | Description |
|---|---|---|
| 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
| Function | Usage | Example |
|---|---|---|
strlen() | String length | strlen("Hello") |
str_replace() | Replace text | str_replace("World", "PHP", $text) |
strpos() | Find substring | strpos($text, "PHP") |
strtolower() / strtoupper() | Case conversion | strtoupper("php") |
trim() | Remove whitespace | trim(" 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
| Variable | Description |
|---|---|
$_GET | Collects data via URL parameters |
$_POST | Collects form data |
$_SESSION | Stores session variables |
$_COOKIE | Stores cookies |
$_FILES | Handles file uploads |
$_SERVER | Server information |
$_REQUEST | Combines 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
| Concept | Syntax | Example |
|---|---|---|
| Output | echo, print | echo "Hi"; |
| Variable | $var_name | $a = 10; |
| Array | array() | $x = array(1,2,3); |
| Loop | for(), while() | for($i=0;$i<5;$i++) |
| Condition | if(), else | if($a>$b) |
| Function | function name(){} | function test(){} |
| Include | include('file.php') | include("head.php"); |
| Class | class 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.

