Monday, 10 February 2014

Important things to remember (Difference between $name and $$name)

 $name is a variable while $$name is a reference variable

$foo = 'bar';
$name = 'foo';

echo $name; // foo
echo $$name; //bar
echo $foo; //bar 

You first assign the value of a variable, $name, to the name of another variable. When you set $name to a value, it will replace that variablename with the value of the variable you provide it.

$test = 'asdf';
$$test = 'I am changing asdf\'s value!';
echo $$test; // I am changing asdf's value!
echo $test; // asdf
echo $asdf; // I am changing asdf's value!


Just copy this piece of code and play around it ...

<?php
$test = 'asdf';
$$test = 'I am changing asdf\'s value!';
echo $$test; // I am changing asdf's value!
echo $test; // asdf
echo $asdf; // I am changing asdf's value!

?>

No comments:

Post a Comment

Thank your for your comment..your submitted coment will be live after admin approval