Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason for not using string interpolation in PHP? [closed]

As far as I can see (browsing popular PHP code in GitHub) there are many people not using string interpolation:

$loader->load($config['resource'].'.xml');

Versus:

$loader->load("{$config['resource']}.xml");

Is there any reason (i.e. performances) for not using string interpolation?

like image 618
gremo Avatar asked Jan 14 '23 16:01

gremo


1 Answers

No, there is no reason not to be using string interpolation in PHP.

While there are tiny differences in how PHP handles string interpolation and concatenation internally, those differences are so small that they are barely measurable in practice, and should therefore be left to PHP to handle.

If or when you see benchmarks, for working with strings in PHP, the thing to look for is usually the iteration count. In order to get measurable results you need to set n to some highly exaggerated value, which never occurs in the real world.

To wrap up, optimizing how you work with strings in PHP does not make much sense. Instead, you should focus on problems that actually have a noticeable effect on the performance of your website; caching, database stuff and, especially, the front end.

like image 148
Sverri M. Olsen Avatar answered Jan 19 '23 11:01

Sverri M. Olsen