AdyBlaze
Latest Article

PHP Variables Explained with Examples (Beginner Guide 2026)

23 Jul 2026

WhatsApp Facebook Telegram

PHP Variables Explained with Examples

Variables are one of the most important concepts in PHP. Every PHP application—whether it’s a simple contact form or a large e-commerce website—uses variables to store and process data.

Whenever you save a user’s name, email address, password, product price, or order details, you’re using variables.

In this tutorial, you’ll learn what PHP variables are, how to create them, the rules for naming them, and how to use them effectively with practical examples.


What is a Variable?

A variable is a named container used to store data that can be accessed and changed while a program is running.

Think of a variable as a labeled storage box.

For example:

Similarly, in PHP:

Variables make programs flexible because their values can change without modifying the code.


Why Do We Need Variables?

Imagine creating a website where the user’s name appears on multiple pages.

Without variables, you would have to write the same name repeatedly.

With variables, you only store the value once:

$name = "John";

Then use it anywhere in your program:

echo $name;

If the user’s name changes, you only update the variable instead of editing every occurrence.


Creating a Variable in PHP

In PHP, every variable starts with the $ (dollar sign).

Example:

<?php

$name = "John";

echo $name;

?>

Output

John

Understanding the Code

Let’s break it down.

$name

This is the variable name.


=

This is the assignment operator. It assigns a value to the variable.


"John"

This is the value stored inside the variable.


echo

The echo statement displays the value stored in the variable.


More Examples

Store Age

<?php

$age = 25;

echo $age;

?>

Output:

25

Store City

<?php

$city = "Lucknow";

echo $city;

?>

Output:

Lucknow

Store Website Name

<?php

$website = "AdyBlaze";

echo $website;

?>

Output:

AdyBlaze

Variable Naming Rules

PHP has a few simple rules for naming variables.

Rule 1: Every Variable Starts with $

Correct:

$name

Incorrect:

name

Rule 2: Must Begin with a Letter or Underscore

Correct:

$userName
$_age

Incorrect:

$123name

Rule 3: Cannot Contain Spaces

Correct:

$firstName

Incorrect:

$first Name

Use camelCase or underscores instead.


Rule 4: Case Sensitive

PHP treats uppercase and lowercase letters differently.

Example:

$name = "John";

$Name = "David";

echo $name;

echo $Name;

Output:

John
David

Here, $name and $Name are two different variables.


Best Practices for Naming Variables

Use meaningful names instead of short or confusing ones.

Good examples:

$studentName

$productPrice

$userEmail

$orderNumber

$totalAmount

Poor examples:

$a

$b

$x

$temp1

Meaningful names make your code easier to understand and maintain, especially in larger projects.


Declaring Multiple Variables

You can create multiple variables in the same program.

Example:

<?php

$firstName = "Adil";

$lastName = "Rasheed";

$country = "India";

echo $firstName;

echo $lastName;

echo $country;

?>

Each variable stores different information, and you can use them independently wherever needed in your application.