Hacker News new | ask | show | jobs
by jeroenhd 1512 days ago
I've never known that "{$var}" was even allowed. All other languages using $ for string interpolation use the "${var}" pattern, often also allowing any expression ("${var / 10 + 2}") within the brackets.

Keeping "{$var}" seems like a strange decision to me. I assume the people who made the decision analysed open source projects and evaluated which option gets used most, but it doesn't feel like the most obvious decision to me.

2 comments

You'd have to rewrite a good portion of all existing PHP code to eliminate the "{$var}" syntax. It is the "right" way to include variables in a string.
PHPStorm/PHPStan (I'm not certain which) will tell me `"My {$var} string"` doesn't need the `{}`, but won't give me the same notice when using `"My {$obj->getVar()} string"`. Personally, I prefer keeping the `{}`.
"{$arr[2]}" for array and object, {} is required.