We commonly use the double $ sign to use the value of a variable as a variable name. For example
| <?php$Jon = ‘Mrs. Merry’; $name = ‘Jon’; echo $$name ; // We can easily use to get the output “Mrs. Merry”; ?> |
Now let’s have a fun.
| <?php$name = ‘Jams’; $$name = ‘Jams Smith’; echo $$name; echo $Jams; ?> |
What will be the out put. Easily we can evaluate it. Both $$name and $Jams will give same out put “Jams Smith”; Now yesterday I was reading the fundamentals of PHP. Lets see another example.
| <? php$name = ‘Ja ms’; $$name = ‘Jams Smith’; echo $$name; echo $Ja ms; ?> |
Now $Ja ms will generate a parser error. Because $Ja ms doesn’t follow the convention of variable declaration.
But what about $$name. Is it no confusing…..
But reality is $$name will print ‘Jams Smith’.
PHP manage $$name itself.
Mentors Award Q3 2008
Top Website Coder
1 response so far ↓
rayhan // September 13, 2007 at 5:53 am |
Hi rubel bhai,
It’s really interesting.