PHP CHAPTER 05

PHP Variables

A variable stores a value that can be used and changed during program execution.

📘

What is it?

A variable stores a value that can be used and changed during program execution.

💡

Why do we use it?

Variables allow applications to store names, marks, fees, products and other data.

⚙️

How does it work?

A PHP variable begins with $ and receives a value using the assignment operator.

🧩

Syntax

PHP Syntax
$name = "value";
$number = 100;
💻

Example

PHP Example
<?php

$name = "Imran";
$age = 25;
$course = "PHP";

 echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Course: " . $course;

?>
🔍

Example Explained

Three variables store different values. The dot operator joins text and variables.
🚀

More Examples

<?php $fee = 35000; $discount = 5000; $finalFee = $fee - $discount; echo $finalFee; ?>

⚠️ Important Rules

Variable names are case-sensitive. $name and $Name are different variables.

❌ Common Mistakes

Forgetting $, misspelling variable names and using variables before assigning values.

🌍 Real-World Use

Variables are used everywhere in PHP applications.

📝 Practice Exercise

Create variables for student name, phone, course, fee and city.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →