A variable is just a storage element.
Variables in PHP are represented by a dollar sign ( $ ) by the name of the variable. The variable name is case-sensitive.
The variable name should be start with letters ( a-zA-Z ) or underscore ( _ ) and it should not start with numeric /number
Below we have given some examples for your reference.
<?php
$x = 'hello';
$$x = 'world';
echo "$x ${$x}";
echo "$x $hello";
?>
Output:
hello world
hello world
<?php
class test {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}
$test = new test();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $test->$bar . "\n";
echo $test->$baz[1] . "\n";
$start = 'b';
$end = 'ar';
echo $test->{$start . $end} . "\n";
$arr = 'arr';
echo $test->$arr[1] . "\n";
echo $test->{$arr}[1] . "\n";
?>
Output:
I am bar.
I am bar.
I am bar.
I am r.
I am B.
<?php
$a = 10;
$b = 15;
echo $c = $a + $b;
function myTest()
{
global $a,$a;
$d=$a+$b;
}
myTest();
echo $d;
function myTest()
{
$GLOBALS['b']=$GLOBALS['a']+$GLOBALS['b'];
}
myTest();
echo $b;
?>
Output:
25
25
25