Good believe you mean variable variable and not variable variable
- What are variables of variables and what they serve?
A variable name that can be configured and used dynamically. A variable takes the value of a variable and treats it as the name of a variable.
- Should we use variable variables? , if yes in what situations?
They are most useful when accessing values within an array property, when the property name is made of multiple parts, or when the property name contains characters that are not valid (e.g., from json_decode() or Simplexml ).
I think the best example to understand its use is this:
<?php
$varname = "foo";
$foo = "bar";
print $$varname; // Prints "bar"
print "$$varname"; // Prints "$foo"
print "${$varname}"; // Prints "bar"
?>
And for me at least, the greatest utility it has is when vc has compound variable names, but that have part of the same name referring to something, so vc puts only a list of the differentiation of these variables in a vector and with a foreach list all, as shown in the example below:
<?php
// Given these variables ...
$nameTypes = array("first", "last", "company");
$name_first = "John";
$name_last = "Doe";
$name_company = "PHP.net";
// Then this loop is ...
foreach($nameTypes as $type)
print ${"name_$type"} . "\n";
// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
- Variable variables, have some effect on php application performance ?
Look if there is improvement in the performance I do not know, but I believe that not, what I know is that it has optimization of code as demonstrated in the previous example.
All examples and exceptions you can find in the official documentation: http://php.net/manual/en/language.variables.variable.php
Related: Variable variable in php
– user28595
Related²: What utility has this way of accessing variable values
– user28595
I had already looked before publishing but I think it does not cover all my questions
– 13dev
I believe all doubts are resolved in the link @Articuno posted. Anyway, I preferred to vote to close as broad and not as duplicate because I understand that the question escapes a little from the proposal of the site (many doubts in a post only). Here is the suggested reading of the [tour]. [Ask], and then [Edit] the question to leave it only with the specific question, if there really is something unsatisfied in the other posts. Remember that when you have questions, you can ask about one part at a time, and post new questions when the previous one is resolved.
– Bacco
Possible duplicate of What is a scalar variable?
– UzumakiArtanis