PHP Cheatsheet

Last Updated : 23 Jul, 2025

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.

php-
PHP Cheatsheet

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
<?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
<?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 $.

FeatureDescription
define()Defines a constant
const keywordAlternative way to define constants (PHP 5.3+)
Case-sensitivityConstants are case-sensitive by default
Global scopeConstants are accessible anywhere after declaration
No $ symbolConstants 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:

TypeDescriptionExample
StringText data enclosed in quotes$name = "Anjali";
IntegerWhole numbers$count = 10;
FloatDecimal numbers (also called double)$price = 15.75;
BooleanTrue or False$is_valid = true;
ArrayCollection of values stored in a single variable$colors = ["red", "blue"];
ObjectInstance of a class with properties and methods$car = new Car();
NULLRepresents a variable with no value assigned$empty = null;
ResourceSpecial variable that holds a reference to external resources like database connections or files