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?

1

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.