A variable stores a value. In PHP every variable starts with a $ sign.
$name = 'Asha';
$age = 21;
$price = 499.50;
$isStudent = true;
Rules
- Starts with
$followed by a letter or underscore - Can contain letters, numbers and underscores — no spaces or dashes
- Case-sensitive:
$age≠$Age - Use clear names in camelCase or snake_case:
$totalPrice,$total_price
No type declaration
PHP decides the type from the value. The same variable can later hold another type.
Variables in strings
Inside double quotes, variables are replaced by their value:
echo "Hello $name, you are $age years old";
Inside single quotes, they are not: 'Hello $name' prints exactly Hello $name.
For arrays and objects use braces: "Course: {$student['course']}".
Example
Example — Create and print variables
PHP
<?php
$name = 'Ravi';
$city = 'Gaya';
$fee = 4999;
echo "Student: $name from $city<br>";
echo 'Single quotes: $name<br>';
echo 'Joined with dot: ' . $name . ' pays ₹' . $fee . '<br>';
$fee = $fee - 500; // change the value
echo "Fee after discount: ₹$fee";
Output: Student: Ravi from Gaya / Single quotes: $name / Joined with dot: Ravi pays ₹4999 / Fee after discount: ₹4499