<28/>
28 Lazy Coder
PHP

PHP Variables: Everything You Need to Know

Featured Image
PHP Variables Explained
The Article

If you’ve already learned how to display output using echo and print, the next thing you need to understand is variables.

Variables are one of the most fundamental concepts in PHP. Whether you’re creating a login system, storing form data, working with databases, or building APIs, you’ll be using variables everywhere.

In this guide, you’ll learn:

By the end of this article, you’ll be comfortable using variables in your own PHP projects.


What is a Variable?

A variable is simply a container used to store data.

Think of it like a labeled box.

For example:

<?php
$name = "Ashutosh";

Here,

Now you can use this value anywhere in your script.

<?php

$name = "Ashutosh";

echo $name;

Output

Ashutosh

Why Do We Need Variables?

Imagine writing this:

echo "Ashutosh";
echo "Ashutosh";
echo "Ashutosh";
echo "Ashutosh";

Now suppose the name changes.

You’ll have to update every occurrence manually.

Instead:

$name = "Ashutosh";

echo $name;
echo $name;
echo $name;
echo $name;

Now changing one line updates every place where the variable is used.


How to Declare a Variable in PHP

Every PHP variable begins with the dollar sign ($).

Syntax:

$variableName = value;

Example:

<?php

$city = "Delhi";
$age = 24;
$salary = 45000;

Unlike languages like Java or C++, PHP does not require you to declare the data type explicitly. PHP determines the variable’s type automatically based on the assigned value.


Variable Naming Rules

PHP has a few simple rules for variable names.

✅ Valid

$name
$userName
$_email
$age2
$totalPrice

❌ Invalid

$2age
$user-name
$first name

Rules:


Variables are Case Sensitive

This is one of the most common beginner mistakes.

$name = "John";

echo $name;
echo $Name;

Output:

John
Undefined variable: Name

Always use consistent naming.


Assigning Values

You can store different kinds of values.

$name = "John";
$age = 25;
$height = 5.8;
$isStudent = true;

PHP automatically detects the correct data type.

We’ll learn more about these in our upcoming guide on PHP Data Types.


Reassigning Variables

Variables can be updated anytime.

$score = 10;

echo $score;

$score = 20;

echo $score;

Output

10
20

This makes variables extremely useful when values change during program execution.


Using Variables Together

Variables can also be combined.

$firstName = "John";
$lastName = "Doe";

echo $firstName . " " . $lastName;

Output

John Doe

The . operator joins strings together.


Variables Inside Strings

PHP supports variable interpolation inside double quotes.

$name = "John";

echo "Hello $name";

Output

Hello John

Single quotes behave differently.

echo 'Hello $name';

Output

Hello $name

Variable Scope

Scope defines where a variable can be accessed.

There are three main scopes.

Local Variable

Declared inside a function.

function greet()
{
    $name = "John";

    echo $name;
}

greet();

The variable exists only inside the function.


Global Variable

Declared outside a function.

$name = "John";

function greet()
{
    global $name;

    echo $name;
}

greet();

Using global allows access to the variable inside the function.


Static Variable

Normally, local variables disappear after a function finishes.

Static variables remember their value.

function counter()
{
    static $count = 0;

    $count++;

    echo $count;
}

counter();
counter();
counter();

Output

1
2
3

Common Beginner Mistakes

Forgetting the Dollar Sign

Wrong

name = "John";

Correct

$name = "John";

Using Variables Before Assignment

Wrong

echo $age;

Correct

$age = 20;

echo $age;

Mixing Uppercase and Lowercase

Wrong

$name = "John";

echo $Name;

Correct

$name = "John";

echo $name;

Best Practices

✔ Use meaningful names

$userName

instead of

$x

✔ Follow a consistent naming style

$totalPrice

is much better than

$tp

✔ Keep names descriptive

Good

$productPrice

Bad

$p

Real-World Example

Suppose you’re building a registration form.

<?php

$name = "Ashutosh";
$email = "ash@example.com";
$city = "Noida";

echo "Name: $name <br>";
echo "Email: $email <br>";
echo "City: $city";

Output

Name: Ashutosh
Email: ash@example.com
City: Noida

This is exactly how data from forms, databases, and APIs is stored and displayed in PHP applications.


What’s Next?

Now that you understand variables, the next logical topic is learning about the different kinds of values they can store.

Continue with:

If you haven’t already, make sure to read our previous guide on Echo vs Print in PHP:

👉 https://28lazycoder.com/echo-vs-print-in-php/


Official PHP Documentation

To dive deeper into variables and advanced concepts such as variable scope and predefined variables, refer to the official PHP documentation:


Conclusion

Variables are the building blocks of every PHP application. They let you store, update, and reuse data efficiently, making your code cleaner and easier to maintain.

Mastering variables now will make upcoming topics like data types, operators, loops, functions, and arrays much easier to understand. Take some time to experiment with the examples above, try changing the values, and observe how your program behaves. The more you practice, the more natural working with variables will become.

AR

Ashutosh Rajbhar

Full-stack developer

3+ years building WordPress, React, and performance-focused web projects.

Previous ← JavaScript Functions: A Complete Guide with Examples