DEV Community

Discussion on: 5 bad habits to lose in PHP

Collapse
 
rehmatfalcon profile image
Kushal Niroula

For the fourth one, I rather prefer string interpolation.

echo "Hello {$name}" ;
Collapse
 
jsn1nj4 profile image
Elliot Derhay

I like string interpolation too whenever I can get away with it.

Collapse
 
marcusatlocalhost profile image
Marcus

String interpolation gets weird if your string contains quotes, though

printf('<a href="%s"></a>',$link);

vs

echo "<a href=\"{$link}\"></a>";

Of course one could write it this way

echo "<a href='{$link}'></a>";
  • but for some reasons I don't like single quotes for html attributes :)

At the end and in this case, whatever works best/is the easiest to write and read. sprintf with many vars is a pain to read.

Collapse
 
klnjmm profile image
Jimmy Klein

String interpolation works to in this case. 👍
I am used to (s)printf when
I want to translate string which contains variables with gettext and also to format float.