PHP Variables and Constants Explained with Examples
Introduction
In every programming language, variables and constants are used to store and manage data — and PHP is no different.
Understanding variables and constants is the foundation of PHP programming. They allow you to store values (like numbers, strings, or arrays) and reuse them anywhere in your code.
In this guide, you’ll learn:
What are PHP variables and constants
How to declare and use them
Naming rules
Key differences between variables and constants
Best practices with examples
What Are Variables in PHP?
A variable is a container that stores data which can change during program execution.
Think of a variable like a labeled box that can hold a value — and you can open it anytime to read or replace what’s inside.
Declaring Variables in PHP
In PHP, a variable always begins with a dollar sign $ followed by the variable name.
Syntax:
$variable_name = value;
Example:
<?php
$name = "Arvinder";
$age = 25;
$city = "Chandigarh";
echo "Name: $name <br>";
echo "Age: $age <br>";
echo "City: $city";
?>
Output:
Name: Arvinder
Age: 25
City: ChandigarhRules for Variable Names
To declare a valid PHP variable:
Must start with a $ sign.
Must start with a letter or underscore, not a number.
Can contain letters, numbers, and underscores (
_).Case-sensitive —
$Nameand$nameare different variables.
Invalid Example:
$9name = "Arvinder"; // Invalid: starts with a numberValid Example:
$_name = "Arvinder"; // ValidVariable Types in PHP
PHP is a loosely typed language, meaning you don’t have to declare a data type. PHP automatically detects it.
| Type | Example | Description |
|---|---|---|
| String | $name = "John"; | Text enclosed in quotes |
| Integer | $age = 25; | Whole numbers |
| Float | $price = 9.99; | Decimal numbers |
| Boolean | $isAdmin = true; | true/false values |
| Array | $colors = ["Red","Green","Blue"]; | Collection of values |
| Object | $person = new Person(); | Instance of a class |
| NULL | $x = NULL; | No value assigned |
Variable Scope in PHP
Scope defines where a variable can be accessed in a PHP program.
| Scope Type | Description |
|---|---|
| Local | Declared inside a function and can only be used there |
| Global | Declared outside functions and available everywhere using global keyword |
| Static | Retains its value between function calls |
Example:
<?php
$globalVar = "I am Global";
function testScope() {
global $globalVar; // access global variable
echo $globalVar;
}
testScope();
?>
Output:
I am GlobalPHP Variable Variables
PHP allows you to use a variable’s value as another variable name.
Example:
<?php
$name = "Arvinder";
$$name = "Welcome to PHP!";
echo $Arvinder;
?>
Output:
Welcome to PHP! Explanation:$$name means “the variable whose name is the value of $name.”
What Are Constants in PHP?
A constant is a name or identifier for a simple value that cannot change during script execution.
Constants are useful for storing fixed values such as tax rates, version numbers, or file paths.
Defining Constants
Constants are defined using:
The
define()function (traditional way)The
constkeyword (modern approach)
Using define():
<?php
define("SITE_NAME", "PHP Learning Portal");
echo SITE_NAME;
?>
Output:
PHP Learning PortalUsing const Keyword:
<?php
const PI = 3.14159;
echo PI;
?>
Output:
3.14159Rules for Constants
No
$sign before constant names.Constants are automatically global.
Once set, their value cannot be changed or undefined.
Difference Between Variables and Constants
| Feature | Variable | Constant |
|---|---|---|
| Symbol | Starts with $ | No $ sign |
| Value | Can change | Fixed once declared |
| Defined by | = operator | define() or const |
| Scope | Local or global | Always global |
| Case Sensitivity | Case-sensitive | Case-sensitive (unless specified otherwise) |
PHP Predefined Constants
PHP provides many built-in constants such as:
<?php
echo PHP_VERSION; // Displays current PHP version
echo PHP_OS; // Displays the operating system
?>
Output Example:
8.3.0 LinuxBest Practices for Variables & Constants
- Use meaningful names —
$userName,$totalPrice - Write constants in uppercase —
SITE_URL,TAX_RATE - Don’t overwrite variables unnecessarily
- Use constants for values that never change
- Maintain consistent naming conventions