1

while reading about variable parsing in double quoted strings in the php manual I came across 2 examples which are confusing to me. An example of how this works with some code would help greatly. Here is the code from the manual:

echo "This works too: {$obj->values[3]->name}";

echo "This is the value of the var named $name: {${$name}}";

echo "This is the value of the var named by the return value of getName(): {${getName()}}";

What exactly do these mean? I know that $obj is an object. I just don't know what would've been the precursor code of these examples. Any help would be useful.

Robert
  • 10,126
  • 19
  • 78
  • 130
  • 1
    possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) –  Aug 13 '14 at 00:01
  • @Dagon I think this question just needs to be renamed... Seems like the content is more about how to interpret variables inside curly brackets (which are inside double quotes), etc. – jerdiggity Aug 13 '14 at 00:06
  • @jerdiggity curly braces are explained in the linked duplicate –  Aug 13 '14 at 00:09
  • @bboy then search on the basics of php OO –  Aug 13 '14 at 00:10

2 Answers2

3

Let's use the 3 examples from the manual you gave.

We can provide these variables a name and look at the output to see what they do.

$obj->values[3]->name = "Object Value";
$name = "variable";
$variable = "Variable Value"; // You'll see why we need to define this in a minute
$function = "Function Value";
function getName() {
    return "function";
}

I imagine you're already seeing where this is going but let's see what this mean for the statements you posted:

echo "This works too: {$obj->values[3]->name}"; // This works too: Object Value

echo "This is the value of the var named $name: {${$name}}"; // This is the value of the var named $name: Variable Value

echo "This is the value of the var named by the return value of getName(): {${getName()}}"; // This is the value of the var named by the return value of getName(): Function Value

In the first case, it replaces the object value with "Object Value."

In the second $name gets interpreted as "variable", which means {${$name}} gets interpreted as the value of $variable, which is "Variable Value."

The same principle applies to the return value of the function.

mAAdhaTTah
  • 400
  • 1
  • 12
  • 2
    Just read this in the manual which after some thought made things better: `Note: Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables.` – Robert Aug 13 '14 at 00:24
0

Here follows the examples you posted,

When you use {}you make sure the entire path of the object gets evaluated. If you remove them it will only evaluate $obj->values which returns Array.

$obj->values[3] = "Name";
echo "This works too: {$obj->values[3]}"."\n"; // CORRECT: This works too: Name
echo "This works too: $obj->values[3]"."\n\n"; // ERROR:   This works too: Array[3]

In the first two examples $name is evaluated first, and you will be left with {$mike}. Since you have the curly braces outside the new variable name, this too will be evaluated, and will translate to the string Michael instead. In the last example you will be left with $mike.

$name = "mike"; 
$mike = "Michael"; 
echo "This is the value of the var named $name: {${$name}}"."\n"; // This is the value of the var named mike: Michael
echo "This is the value of the var named $name: {$$name}"."\n";   // This is the value of the var named mike: Michael
echo "This is the value of the var named $name: $$name"."\n\n";   // This is the value of the var named mike: $mike

This example is similar to the one above, but instead of using the variable $name, a function is used instead. If you do not have the curly braces {} around the function (example #2), but around $getName(), PHP will try to to access the function $getName, which is not a legal function name. The last example $getName() will fetch the value of the variable $getName and the () will be left alone. So if you would have $getName = "Heya";, it would become Heya().

function getName() { return "mike"; }
echo "This is the value of the var named by the return value of getName(): {${getName()}}"; // This is the value of the var named by the return value of getName(): Michael
echo "This is the value of the var named by the return value of getName(): {$getName()}";  // Fatal error: Function name must be a string
echo "This is the value of the var named by the return value of getName(): $getName()";     // This is the value of the var named by the return value of getName(): ()
Erlesand
  • 1,525
  • 12
  • 16