agyanadda@gmail.com

PHP Variables

PHP Basics 1 min read Updated Dec 1, 2026 Topic 4 of 36

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

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which is a valid PHP variable name?

Exercise 2 True / False

Variables are replaced by their values inside single quotes.

Exercise 3 Fill in the Blank

Every PHP variable starts with the ___ sign.

Frequently Asked Questions

You used a variable before giving it a value, or misspelled it (remember names are case-sensitive).