Interesting variable assigning in PHP

26 Aug

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. 🙂

Advertisements

One Response to “Interesting variable assigning in PHP”

  1. rayhan September 13, 2007 at 5:53 am #

    Hi rubel bhai,
    It’s really interesting.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: