In PHP, every variable starts with a dollar sign ($). This helps the computer know you are using a variable.
When you want to store a number or text to use later.
When you need to remember a user's name or age.
When you want to keep track of a score in a game.
When you want to save a result from a calculation.
When you want to change the value and use it again.
Syntax
PHP
$variable_name = value;
The dollar sign ($) must come before the variable name.
Variable names can have letters, numbers, and underscores but cannot start with a number.
Examples
This creates a variable called $name and stores the text "Alice" in it.
PHP
$name = "Alice";
This creates a variable called $age and stores the number 25.
PHP
$age = 25;
This creates a variable called $score and starts it at zero.
PHP
$score = 0;
Sample Program
This program creates two variables, $name and $age, then uses them inside a greeting message. It prints the message to the screen.
PHP
<?php
$name = "Bob";
$age = 30;
$greeting = "Hello, $name! You are $age years old.";
echo $greeting;
?>
OutputSuccess
Important Notes
Always start variable names with a letter or underscore, never a number.
Variable names are case-sensitive: $Name and $name are different.
You can change the value of a variable anytime by assigning a new value.
Summary
All PHP variables start with a dollar sign ($).
Variable names store data like text or numbers.
Use variables to keep and reuse information in your program.
Practice
(1/5)
1. Which of the following is the correct way to declare a variable named age in PHP?
easy
A. $age = 25;
B. age = 25;
C. var age = 25;
D. int $age = 25;
Solution
Step 1: Recognize PHP variable syntax
In PHP, all variables must start with a dollar sign ($) followed by the variable name.
Step 2: Check each option for correct syntax
$age = 25; uses $age = 25; which is correct. Options B and C miss the dollar sign, and A uses a type declaration which is not standard in PHP variable declaration.
Final Answer:
$age = 25; -> Option A
Quick Check:
PHP variables start with $ = $age = 25; [OK]
Hint: Always start PHP variables with $ sign [OK]
Common Mistakes:
Forgetting the $ sign before variable name
Using other language syntax like var or int
Trying to declare type explicitly in PHP
2. Which of the following is a syntax error in PHP variable declaration?
easy
A. $1number = 10;
B. $name = "John";
C. $total = 100;
D. $_score = 50;
Solution
Step 1: Understand valid variable names in PHP
Variable names must start with a letter or underscore, not a number.
Step 2: Identify invalid variable name
$1number = 10; uses $1number which starts with a number, causing a syntax error. Other options start with $ followed by letter or underscore, which is valid.