scanreg Posted October 27, 2009 Share Posted October 27, 2009 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 Quote Link to comment Share on other sites More sharing options...
onlyican Posted October 27, 2009 Share Posted October 27, 2009 I double dollar sign is handy for getting a variable name in a variable $CarMake = "Ford"; $CarColor = "Silver"; $$GetVar = "CarMake"; echo $$GetVar; Result = "Ford" Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2009 Share Posted October 27, 2009 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. Quote Link to comment Share on other sites More sharing options...
onlyican Posted October 27, 2009 Share Posted October 27, 2009 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) Quote Link to comment Share on other sites More sharing options...
scanreg Posted October 27, 2009 Author Share Posted October 27, 2009 Wow, thanks so much! You've all hit bulls-eye points, thanks again Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 Variable variables are three times slower than using array variables. Nevermind the performance, it makes your code almost entirely unreadable. Quote Link to comment Share on other sites More sharing options...
scanreg Posted October 27, 2009 Author Share Posted October 27, 2009 How does $$GetVar know that "CarMake" refers to the variable $CarMake ? Is GetVar a function? I couldn't find it on php.net Many thanks Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 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; Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 27, 2009 Share Posted October 27, 2009 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. Quote Link to comment Share on other sites More sharing options...
scanreg Posted October 27, 2009 Author Share Posted October 27, 2009 I think I get it, thank you thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.