I'm trying to modify the name of the key dynamically based on which rows are being fetched, but my syntax seems to be slightly off within the query. After moving the quotes around more times than I care to admit, I finally decided it was time to ask for help ;-)
$var = '$foo_row';
$MAX_5A = ${$var . '["MAX_5A"]'};
Instead of
$MAX_5A = $foo_row['MAX_5A'];
Bonus points if someone wants to explain to me the logic behind the correct syntax :-)
This should work for you:
(Just use variable variables with curly quotes to make sure PHP doesn't think this: ${$var["MAX_5A"]}. Also note I removed the dollar sign in the string)
$var = 'foo_row';
//^ dollar sign removed
$MAX_5A = ${$var}["MAX_5A"];
$var = 'foo';
$bar = 'var';
echo $$bar; // foo
Logic: A variable variable takes the value of a variable and treats that as the name of a variable.
Related
Suppose I have the following:
$foo = "bar";
$bar = "hello";
Then the string "hello" can be echoed to standard output as either:
echo $$foo;
or
echo ${$foo};
I was wondering what the difference is between these two statements in general.
That is, what is the purpose of the braces around the evaluated variable name
in the second syntax displayed above?
The only reason to use ${$foo} is to be able to combine $foo to something else, for example:
$idx = 4;
$bar3 = 20;
$bar4 = 7;
echo ${$foo.$idx}
This would return the value in $bar4 since $idx is worth 4;
There is no difference between the two in what they do in YOUR example.
The first example is concept knows as Variable Variables while the second braces example allows you to generate dynamic variables based on specified values.
You may also want to checkout:
PHP Variable Names: Curly Brace Madness
Accessibility and Dynamic Variable names
I'm new to PHP so please forgive me if this is a stupid question.
How do you include a variable in a variable?
What I mean is:
<?php
$variable_a = 'Adam';
$variable_b = '$variable_a';
?>
In other words the second variable is the same as the first one.
I won't bother explaining why I need to do it (it will confuse you!), but I just want to know firstly if it's possible, and secondly how to do it, because I know that code there doesn't work.
Cheers,
Adam.
Don't use the quotes, they indicate a string. Just point to the variable directly, like this:
$variable_b = $variable_a;
If you want the variables to be equal, use:
$variable_b = $variable_a;
If you want the second variable to contain the first, use variable parsing:
$variable_b = "my other variable is: $variable_a";
Or concatenation:
$variable_b = 'my other variable is: ' . $variable_a;
PHP have this advantage in producing one string variable's value based on another. To do this, write code like this:
$b = "My name is $name.";
The following code does NOT work:
$b = '$name';
Other occasions in which coding like this works are:
$b = <<<STRING
Hello, my name is $name...
STRING;
If you want to access an array, use:
$b = "My ID is {$id['John Smith']}.";
and of course,
$b = <<<STRING
Hello, my name is {$username}, my ID is {$id['John Smith']}.
STRING;
I recommend using {} because I frequently use Chinese charset in which occasion coding like
$b = "我是$age了。";
will cause PHP look up for variable $age了。 and cause error.
Either without quotes to reference the variable directly, since quotations means it's a string
$variable_b = $variable_a;
Or you can ommit the variable in double quotations, if you want it to appear in a string.
$variable_b = "My name is $variable_a";
I was showing someone how you can create variable variable variables in PHP (I'd only recommend using them NEVER, it's horrible practice and you are a bad person if you use variable variable variables in actual production code), and they asked if the dollar sign acted as a dereference operator in this case.
It doesn't actually create a reference to the other variables, so I don't really see it as being the deref op. The documentation for variable variables doesn't even mention references at all.
Who's right? I don't think variable variables are creating references and therefore the dollar sign isn't the dereference operator.
Here's some sample code for your viewing pleasure (or pain given the contents):
<?php
$a = 'c';
$b = 'a';
$c = 'hello';
echo($$$b); //hello
Is the dollar sign in a variable variable considered the dereference
operator?
No. PHP does not possess a dereference operator.
Variable variables shouldn't be thought of as dereferencing but, rather, accessing the symbol tree via a string. For example:
$bar = 1;
echo ${'bar'};
You can perform this dynamically by using a variable instead of a string literal:
$bar = 1;
$foo = 'bar';
echo ${$foo};
PHP syntax allows you to remove the braces but it's still a matter of accessing the symbol table via a string. No referencing/dereferencing involved.
No, it is not DE-referencing anything....if anything at all, it is referencing the reference of a stored variable name to reference the stored variable name's stored value....kind of a double reference or reference of the reference.....de-ref would mean that one variable was part of the subset of another.
I understand what the following line does but i don't understand how the brackets are used? I have always used brackets in an if, while and other statements but i have never used them in this fashion.
Are there rules to using them this way, should i not use them in this way? Any help would be appreciated... Thanks
${$key} = $temp;
In that specific case, there is effectively no difference between using brackets and not.
So your code is equivalent to the following:
$$key = $temp;
The brackets are typically used to force PHP to interpolate variables in strings, which isn't necessary in this case.
Using the brackets is very helpful for reducing ambiguity in a statement using array indices:
${$array[0]} = $temp;
As opposed to
$$array[0] = $temp;
The parser will think that you meant ($$array)[0], not $($array[0])
Have a look at:
Variable variables
Today I learned about PHP variable variables; "variable variable takes the value of a variable and treats that as the name of a variable". Also, variable
It seems to be using variable variables.
Otherwise, braces like that are usually used for variable interpolation in strings, where the variable is an object property or array member.
In the example above, they are not necessary. It also seems you can subscript an array with variable variables, with no issues.
I get the basics of variable variables, but I saw a syntax just know, which bogles my mind a bit.
$this->{$toShow}();
I don't really see what those {} symbols are doing there. Do they have any special meaning?
PHP's variable parser isn't greedy. The {} are used to indicate what should be considered part of a variable reference and what isn't. Consider this:
$arr = array();
$arr[3] = array();
$arr[3][4] = 'Hi there';
echo "$arr[3][4]";
Notice the double quotes. You'd expect this to output Hi there, but you actually end up seeing Array[4]. This is due to the non-greediness of the parser. It will check for only ONE level of array indexing while interpolating variables into the string, so what it really saw was this:
echo $arr[3], "[4]";
But, doing
echo "{$arr[3][4]}";
forces PHP to treat everything inside the braces as a variable reference, and you end up with the expected Hi there.
They tell the parser, where a variable name starts and ends. In this particular case it might not be needed, but consider this example:
$this->$toShow[0]
What should the parser do? Is $toShow an array or $this->$toShow ? In this case, the variable is resolved first and the array index is applied to the resulting property.
So if you actually want to access $toShow[0], you have to write:
$this->{$toShow[0]}
These curly braces can be used to use expressions to specify the variable identifier instead of just a variable’s value:
$var = 'foo';
echo ${$var.'bar'}; // echoes the value of $foobar
echo $$var.'bar'; // echoes the value of $foo concatenated with "bar"
$this->{$toShow}();
Break it down as below:
First this is a object oriented programming style as you got to see $this and ->. Second, {$toShow}() is a method(function) as you can see the () brackets.
So now {$toShow}() should somehow be parsed to a name like 'compute()'. And, $toShow is just a variable which might hold a possible function name. But the question remains, why is {} used around.
The reason is {} brackets substitues the value in the place. To clarify,
$toShow="compute";
so,
{$toShow}(); //is equivalent to compute();
but this is not true:
$toShow(); //this is wrong as a variablename is not a legal function name