PHP Variables
PHPVariables in PHP start with a $ sign. No need to declare the type — PHP figures it out automatically.
Rules
- Must start with
$followed by a letter or underscore - Case-sensitive:
$nameand$Nameare different - No need to declare type (string, int, etc.)
Example — PHP
<?php
$name = "Rahul";
$age = 20;
$height = 5.8;
$isStudent = true;
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Height: " . $height . " feet<br>";
echo "Student: " . ($isStudent ? "Yes" : "No");
?>
Result
Name: Rahul
Age: 20
Height: 5.8 feet
Student: Yes
Age: 20
Height: 5.8 feet
Student: Yes
Interview Questions — PHP Variables
5 questions commonly asked in interviews
Q1
What are variables in PHP?
Beginner
Variables store data values and start with $ symbol.
Q2
How to declare variable?
Beginner
Use $ followed by name, e.g., $x = 10;
Q3
Is PHP typed language?
Intermediate
PHP is loosely typed.
Q4
What is scope?
Intermediate
Scope defines where variable can be accessed.
Q5
What is global keyword?
Advanced
Used to access global variable inside function.
Frequently Asked Questions
Common doubts about PHP Variables
No, PHP auto-detects type.
No.
Yes for variables.
Yes.
Value that cannot change.
Test Your Knowledge
5 questions · Earn 50 XP
0
/ 5
Q1
Variable starts with?
Q2
Which is valid?
Q3
PHP is?
Q4
Scope defines?
Q5
Global keyword is used?
More on PHP Variables
Cheatsheet, tips, resources & what to learn next
Quick Cheatsheet
Variable
$x = 10;
String
$name = \"John\";
Global
global $x;
Constant
define(\"PI\", 3.14);
Echo
echo $x;
Pro Tips
1
Always use $ symbol.
2
Use meaningful names.
3
Avoid global variables.
4
Use constants when needed.
5
Keep variables simple.