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:
- What PHP variables are
- How to create them
- Variable naming rules
- Best practices
- Variable scope
- Common mistakes beginners make
- Real-world examples
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.
- Put a value inside the box.
- Give the box a name.
- Use that value whenever you need it.
For example:
<?php
$name = "Ashutosh";
Here,
$nameis the variable."Ashutosh"is the value stored inside it.
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:
- Must start with
$ - First character after
$must be a letter or underscore - Cannot start with a number
- Can contain letters, numbers, and underscores
- Variable names are case-sensitive (
$nameand$Nameare different variables)
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:
- PHP Data Types (Coming Soon)
- PHP Operators (Coming Soon)
- PHP Loops (Coming Soon)
- PHP Functions (Coming Soon)
- PHP Constants (Coming Soon)
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:
- https://www.php.net/manual/en/language.variables.php
- https://www.php.net/manual/en/language.variables.basics.php
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.