Variables
Learn PHP variables — named storage with $, naming rules, real-world examples, and best practices for readable backend code.
Welcome to Variables
Imagine you're building an online shopping website.
A customer enters their name, email address, and shipping location before placing an order.
How does your application remember this information while processing the order?
The answer is variables.
Variables allow PHP to temporarily store information in memory so it can be used whenever it's needed. Every PHP application, from a simple contact form to a large banking system, relies on variables to process data.
What is a Variable?
A variable is a named storage location used to hold data during the execution of a program.
Think of a variable as a labeled container.
Instead of remembering the actual value, you remember the container's name.
For example:
- Customer Name
- Product Price
- Total Amount
- Order Status
The value inside the container can change whenever your program needs it.
In PHP, every variable starts with the $ (dollar) symbol.
Why Do We Need Variables?
Imagine creating an online food delivery application.
When a customer places an order, the application needs to remember:
- Customer name
- Restaurant name
- Order amount
- Delivery address
- Payment status
Without variables, every value would need to be hardcoded into the program, making the application impossible to use in the real world.
Variables allow each customer to have their own unique data.
Creating Your First Variable
Creating a variable in PHP is simple.
<?php$name = "Rahul";echo $name;?>
Output
Rahul
Code Explanation
$name = "Rahul";
Creates a variable named name and stores the value "Rahul".
echo $name;
Displays the value stored inside the variable.
Notice that we don't write the quotation marks when printing the variable itself.
Variables Can Store Different Values
Variables aren't limited to text.
They can store numbers, decimal values, and even true or false values.
<?php$product = "Laptop";$price = 65000;$discount = 15;$available = true;echo $product;?>
Every variable stores a specific type of information that your application can use later.
Real-World Example
Let's create a simple employee profile.
<?php$employeeName = "Amit Kumar";$department = "Finance";$salary = 75000;echo "Employee: " . $employeeName;echo "<br>";echo "Department: " . $department;echo "<br>";echo "Salary: ₹" . $salary;?>
Output
Employee: Amit Kumar Department: Finance Salary: ₹75000
This is exactly how business applications display information stored in variables.
Variable Naming Rules
PHP variables must follow a few simple rules.
Valid Examples
$name$productPrice$totalAmount$employeeId
Invalid Examples
$1name$total-price$user name
Remember
- Every variable starts with $
- Variable names are case-sensitive
- Use meaningful names
- Don't use spaces
- Don't use special characters except _
Variable Naming Best Practices
Good variable names make your code easier to understand.
Poor Example
$a = "Rahul";$b = 50000;
It's difficult to understand what a and b represent.
Better Example
$customerName = "Rahul";$orderAmount = 50000;
The purpose of each variable is immediately clear.
Best Practices
- Use descriptive variable names.
- Follow a consistent naming style (camelCase is common in PHP).
- Store one value per variable.
- Keep variable names short but meaningful.
- Avoid unnecessary abbreviations.
Common Mistakes
Forgetting the $ symbol
name = "Rahul";
Correct
$name = "Rahul";
Using spaces
$user name
Correct
$userName
Using unclear variable names
$x$temp$data1
Instead, use names that describe the stored value.
Hands-on Exercise
Task: Create variables for Student Name, Course Name, Course Fee, and Course Duration. Display all the values on the screen.
Example Output
Student: Priya Course: PHP Mastery Fee: ₹1500 Duration: 30 Days
Challenge: Create variables for a product — Product Name, Price, Discount, and Final Price. Display them in a neat format.
<?php// TODO: complete the task and challenge$studentName = "Priya";$courseName = "PHP Mastery";// ...?>
Summary
- Variables are used to store data temporarily during program execution.
- Every PHP variable begins with the $ symbol.
- Variables can store different types of values.
- Meaningful variable names improve code readability.
- Variables are one of the most fundamental concepts in PHP programming.
Key Takeaways
- Variables store information that your application needs.
- PHP variables always begin with $.
- Choose clear and descriptive variable names.
- Variables can hold text, numbers, and other data types.
- Every PHP application uses variables extensively.
Interview Questions
Beginner
Q1What is a variable in PHP?
Q2Why do PHP variables start with the $ symbol?
Q3Can a variable's value change during program execution?
Q4Are PHP variable names case-sensitive?
Q5What happens if you forget the $ symbol?
Intermediate
Q6What are the rules for naming variables in PHP?
Q7Why are meaningful variable names important?
Q8Explain the difference between $userName and $username.
Advanced
Q9How are variables stored during the execution of a PHP script?
Q10How does variable scope affect accessibility within functions?
What's Next?
Now that you know how to store information using variables, the next step is understanding what kind of information those variables can hold.
In the next lesson, Data Types, you'll learn about strings, integers, floats, booleans, and how PHP decides what type of data each variable contains.