What is the difference between $var and $$var in PHP?
$var is a normal variable. $$var is called a variable variable. PHP takes the value stored in $var and uses it as the name of another variable.
<?php
$var = "city";
$$var = "Delhi"; // same as $city = "Delhi";
echo $var . "<br>";
echo $$var . "<br>";
echo $city;
?>
Output:
Tutorials Class - Output Window
city
Delhi
Delhi
Variable variables are hard to read and debug, so use them rarely. In most cases an array is a better choice. See PHP Variables for the basics.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Variables |
| PHP Data Types |
| PHP Constants |
| PHP isset() Function |
| PHP empty() Function |
| PHP is_null() Function |
| Exercises & Assignments |
|---|
| Write a program to print “Hello PHP” using variable |
| Write a program to print a string using echo+variable. |
| Write a program to print two variables in single echo |