objN00B Posted March 14, 2009 Share Posted March 14, 2009 I'm new to PHP!~ and I'm confused with Indirect References to Variables ??? In PHP5 Power Programming they mention 'Indirect References to Variables' with the following example: $name = "John"; $$name = "Registered user"; print $John; I'm having trouble grasping this concept. What is the purpose? Why would I want to create a variable with the name of a variables value? How would the coder know what the initial variable's value is to use the new variable created by name at design time? The only possible place I can see this useful is a using a PHP script to produce a PHP script at runtime. Is this the only practical usefulness of this feature? Quote Link to comment https://forums.phpfreaks.com/topic/149385-solved-indirect-references-to-variables/ Share on other sites More sharing options...
premiso Posted March 14, 2009 Share Posted March 14, 2009 Variables Variable. It is not really used too often, cause an array does it more efficient. To grasp the concept, the extra $ tells it to dynamically assign the variable of the contents of the variable to that. So Since $name = "John"; $$name = "blah"; sets $John to be "blah". If you want to read more on it, search php variables variable. I would not go too deep into it, as it is inefficient and really has few (if any) "practical" uses. Arrays basically do this with associative indexes and is way more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/149385-solved-indirect-references-to-variables/#findComment-784587 Share on other sites More sharing options...
objN00B Posted March 14, 2009 Author Share Posted March 14, 2009 I understood how to use it. I was just asking where one would use this, since I'm currently unaware of any ways to use a undeclared variable declared at runtime, unless you were writing to another .php file of course. Quote Link to comment https://forums.phpfreaks.com/topic/149385-solved-indirect-references-to-variables/#findComment-784590 Share on other sites More sharing options...
PFMaBiSmAd Posted March 14, 2009 Share Posted March 14, 2009 Most programming languages would call it a pointer (most programming languages would also require you to declare a variable before you use it) and you would only be able to use it to put a value into or retrieve a value from an existing data structure. Php is pretty unique in allowing a variable variable to create the actual variable in addition to put a value into it or retrieve a value from it. In addition to the actual execution of a variable variable statement being 3.x times slower than using an array, a variable variable is commonly used to generate a numeric sequence of variables $var1, $var2, ... This is doubly inefficient because you must now keep track of how many variables were created and then use some more inefficient code to process the resulting sequence of variables, instead of some simple, straightforward, and faster code to operate on an array. Quote Link to comment https://forums.phpfreaks.com/topic/149385-solved-indirect-references-to-variables/#findComment-784609 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.