Jump to content

[SOLVED] Indirect References to Variables


objN00B

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/149385-solved-indirect-references-to-variables/
Share on other sites

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.

 

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.

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.