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: Chandigarh

Rules for Variable Names

To declare a valid PHP variable:

  1. Must start with a $ sign.

  2. Must start with a letter or underscore, not a number.

  3. Can contain letters, numbers, and underscores (_).

  4. Case-sensitive$Name and $name are different variables.

Invalid Example:

 

$9name = "Arvinder"; // Invalid: starts with a number

Valid Example:

 

$_name = "Arvinder"; // Valid

Variable Types in PHP

PHP is a loosely typed language, meaning you don’t have to declare a data type. PHP automatically detects it.

TypeExampleDescription
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 TypeDescription
LocalDeclared inside a function and can only be used there
GlobalDeclared outside functions and available everywhere using global keyword
StaticRetains its value between function calls

Example:

<?php
$globalVar = "I am Global";

function testScope() {
   global $globalVar; // access global variable
   echo $globalVar;
}

testScope();
?>

Output:

 
I am Global

PHP 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 const keyword (modern approach)

Using define():

<?php
define("SITE_NAME", "PHP Learning Portal");
echo SITE_NAME;
?>

Output:

 
PHP Learning Portal

Using const Keyword:

<?php
const PI = 3.14159;
echo PI;
?>

Output:

 
3.14159

Rules for Constants

  1. No $ sign before constant names.

  2. Constants are automatically global.

  3. Once set, their value cannot be changed or undefined.

Difference Between Variables and Constants

FeatureVariableConstant
SymbolStarts with $No $ sign
ValueCan changeFixed once declared
Defined by= operatordefine() or const
ScopeLocal or globalAlways global
Case SensitivityCase-sensitiveCase-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
Linux

Best 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