9

What is the difference between "${varname}" and "{$varname}" in PHP's string interpolation? (notice the position of the $)

The php docs say that you can use either, but doesn't clearly explain what the difference between these two methods is. Is there actually a difference?

RPichioli
  • 3,245
  • 2
  • 25
  • 29
nevada_scout
  • 971
  • 3
  • 16
  • 37

1 Answers1

4

The first one is interpolation plus variable variable (dynamic variable), meaning you can use expressions here to define the name of the variable you want to interpolate "${func()}" While the second one syntax is used to distinct variable from the text "some{$variable}text". You can actually combine them:

function func(){
    return 'foo';
}
$foo = 'bar';
echo "some{${func()}}text";

Outputs: somebartext

Alexey Chuhrov
  • 1,787
  • 12
  • 25