Jump to content

[SOLVED] Trying to understand double dollar sign $$variable global


scanreg

Recommended Posts

I'm trying to understand double-dollar-sign variables

 

I came across the following in the php.net docs:

 

http://www.php.net/manual/en/language.variables.scope.php#29273

 

It shows the following:

 

It's possible to use a variable variable when specifying a variable as global in a function. That way your function can decide what global variable to access in run-time.

 

<?php

function func($varname)

{

  global $$varname;

 

  echo $$varname;

}

 

$hello = "hello world!";

func("hello");

?>

 

This will print "hello world!", and is roughly the same as passing by reference, in the case when the variable you want to pass is global. The advantage over references is that they can't have default parameters. With the method above, you can do the following.

 

<?php

function func($varname = FALSE)

{

  if ($varname === FALSE)

    echo "No variable.";

  else

  {

    global $$varname;

 

    echo $$varname;

  }

}

 

$hello = "hello world!";

func("hello");                  // prints "hello world!"

func();                          // prints "No variable."

?>

 

I'm pretty sure it's a way to assign one variable's value to another variable.

 

Would anyone mind explaining it a little more?

 

I'm not getting how func("hello"); prints "hello world!" if function func($varname = FALSE) - doesn't that just make $varname equal to FALSE ?

 

Many thanks :)

Just because you can do something in a programming language, does not mean you should do it.

 

Variable variables are three times slower than using array variables. In almost every case where you have a set of data that you could access using variable variables, an array should be used and would result in the most efficient code.

with regards to

function func($varname = FALSE)

 

Basically, if you do not parse a value for varname, it becomes false

 

func("hello")

$varname == "hello"

func();

$varname == false;

 

I hate this, I think it is bad practice (but handy when you need to parse a new variable in a function for a large website)

 

Well, you can see it like this: $GetVar is substituted with CarMake, so you end up with $CarMake.

 

It's essentially because a variable in PHP can have any name that can be represented using the string data type, so you can also do stupid shit like this:

<?php
$hello = 'test';
${'this is a ' . $hello} = 'foo';
$bar = 'this is a test';
echo $$bar;

it will check to see if there is a variable by that name.  $$GetVar could be anything:  $$GoLeafs, or $$HabsSuck .. it's whatever you want to make it.

 

personally, i do not see the need for it.  why recreate something that is already set.  doesn't ooze efficiency if you ask me.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.