PHP Mastery Tutorial 0/120 lessons ~6 min read Lesson 6

    Variables

    Learn PHP variables — named storage with $, naming rules, real-world examples, and best practices for readable backend code.

    Course progress0%
    Focus
    15 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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
    <?php
    $name = "Rahul";
    echo $name;
    ?>

    Output

    Rahul

    Code Explanation

    php
    $name = "Rahul";

    Creates a variable named name and stores the value "Rahul".

    php
    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
    <?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
    <?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

    php
    $name
    $productPrice
    $totalAmount
    $employeeId

    Invalid Examples

    php
    $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

    php
    $a = "Rahul";
    $b = 50000;

    It's difficult to understand what a and b represent.

    Better Example

    php
    $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

    php
    name = "Rahul";

    Correct

    php
    $name = "Rahul";

    Using spaces

    php
    $user name

    Correct

    php
    $userName

    Using unclear variable names

    php
    $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
    <?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?
    A named storage location in memory that holds a value your program can read, change, and use during script execution. In PHP, variable names are prefixed with $.
    Q2Why do PHP variables start with the $ symbol?
    The $ tells the PHP parser that the identifier is a variable — distinguishing it from constants, function names, and language keywords.
    Q3Can a variable's value change during program execution?
    Yes. Variables are mutable — you can assign a new value to the same name at any point, e.g. $status = 'pending'; then $status = 'shipped';
    Q4Are PHP variable names case-sensitive?
    Yes. $name, $Name, and $NAME are three different variables. Always use consistent casing.
    Q5What happens if you forget the $ symbol?
    PHP will not treat it as a variable. It may look for a constant, throw an undefined constant notice, or produce a parse error depending on context.

    Intermediate

    Q6What are the rules for naming variables in PHP?
    Must start with $ followed by a letter or underscore; subsequent characters can be letters, numbers, or underscores; no spaces; cannot start with a number after $.
    Q7Why are meaningful variable names important?
    They make code self-documenting, reduce bugs during maintenance, help teammates understand logic quickly, and survive code reviews in professional teams.
    Q8Explain the difference between $userName and $username.
    They are two completely different variables because PHP variable names are case-sensitive. Mixing them is a common source of subtle bugs.

    Advanced

    Q9How are variables stored during the execution of a PHP script?
    Values live in memory for the duration of the request. PHP allocates space when you assign; when the script ends, request-scoped variables are freed unless persisted to session, cache, or database.
    Q10How does variable scope affect accessibility within functions?
    Variables declared inside a function are local to that function. Variables outside are not automatically visible inside unless passed as parameters or declared global. Scope is covered in depth in a later lesson.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.