PHP stands for the Hypertext Preprocessor. It is a popular, open-source server-side scripting language designed for web development but also used as a general-purpose programming language. It is widely used to create dynamic web pages and can be embedded into HTML. PHP code is executed on the server, generating HTML that is then sent to the client.
This article provides an in-depth PHP Cheat Sheet, a must-have for every web developer.

What is a PHP Cheatsheet?
A PHP Cheatsheet is a quick reference guide that shows the most important PHP commands, syntax, and examples all in one place. It helps programmers remember how to write PHP code easily without searching through long documents.
It’s like a summary of the basic and useful PHP things you need to know.
PHP Features
Here are some of the main features of PHP:
- Server-Side Scripting – Executes on the server to generate dynamic content.
- Simple Syntax – Easy to learn with C-like syntax.
- Cross-Platform – Runs on various operating systems like Linux, Windows, and macOS.
- Embedded in HTML – Easily integrated into HTML code.
- Supports Databases – Works with MySQL, PostgreSQL, SQLite, and others.
- Rich Built-in Functions – Provides thousands of built-in web and file handling functions.
- Object-Oriented – Supports classes, inheritance, interfaces, and traits.
- Large Community – Extensive support and numerous frameworks like Laravel, Symfony.
1. Basic Syntax
PHP scripts start with <?php and end with ?>.
<?php
echo "Hello, World!";
?>
Output
Hello, World!
2. Variables and Constants
Variables store data values. A variable starts with a $ sign followed by the name. PHP variables are dynamically typed, meaning you don’t need to declare their type.
| Variable | Description |
|---|---|
$name = "PHP"; | String value |
$age = 25; | Integer value |
$price = 9.99; | Float (decimal number) |
$is_active = true; | Boolean (true or false) |
<?php
$name = "PHP";
$age = 25;
$is_active = true;
echo $name . "\n"; // Outputs: PHP
echo $age; // Outputs: 25
?>
Output
PHP 25
Constants
Constants in PHP are fixed values that do not change during script execution. They are defined using define() or const. They are globally accessible and do not begin with $.
| Feature | Description |
|---|---|
define() | Defines a constant |
const keyword | Alternative way to define constants (PHP 5.3+) |
| Case-sensitivity | Constants are case-sensitive by default |
| Global scope | Constants are accessible anywhere after declaration |
No $ symbol | Constants are written without a dollar sign |
3. Magic Constants
Magic constants in PHP are built-in constants that change based on context, like file name, line number, or function name.
Magic Constant | Description |
|---|---|
__LINE__ | Current line number of the file |
__FILE__ | Full path and filename of the file |
__DIR__ | Directory of the file |
__FUNCTION__ | Name of the current function |
__CLASS__ | Name of the current class |
__TRAIT__ | Name of the current trait |
__METHOD__ | Name of the current method |
__NAMESPACE__ | Name of the current namespace |
ClassName::class | It gives the complete name of the class, including its namespace, as a text value (string). |
4. Data Types
PHP supports several data types used to store different kinds of values:
| Type | Description | Example |
|---|---|---|
| String | Text data enclosed in quotes | $name = "Anjali"; |
| Integer | Whole numbers | $count = 10; |
| Float | Decimal numbers (also called double) | $price = 15.75; |
| Boolean | True or False | $is_valid = true; |
| Array | Collection of values stored in a single variable | $colors = ["red", "blue"]; |
| Object | Instance of a class with properties and methods | $car = new Car(); |
| NULL | Represents a variable with no value assigned | $empty = null; |
| Resource | Special variable that holds a reference to external resources like database connections or files |