Difference between $var and $$var in PHP
Last Updated :
18 Oct, 2021
In PHP, $var is used to store the value of the variable like Integer, String, boolean, character. $var is a variable and $$var stores the value of the variable inside it.
$var:
Syntax:
$variable = value;
- The $variable is the variable name
- The value is the initial value of the variable.
Example 1: This example stores and displays values with $.
PHP
<?php
$value1 = "hello Geeks" ;
echo $value1 ;
echo "<br/>" ;
$value2 = true;
echo $value2 ;
echo "<br/>" ;
$value3 = 34;
echo $value3 ;
echo "<br/>" ;
?>
|
Output
hello Geeks<br/>1<br/>34<br/>
$$var: $$var stores the value of $variable inside it.
Syntax:
$variable = "value";
$$variable = "new_value";
- $variable is the initial variable with the value.
- $$variable is used to hold another value.
We can get another value by using the $value of the first variable.
Example 2: PHP program to demonstrates $$var.
PHP
<?php
$value1 = "hello" ;
echo $value1 ;
echo "\n" ;
$ $value1 = "Hello php" ;
echo "$hello" ;
?>
|
Difference between Both: The variable $var is used to store the value of the variable and the variable $$val is used to store the reference of the variable.
Share your thoughts in the comments
Please Login to comment...