1.2.1336. Use PHP7 Encapsed Strings

PHP 7 has optimized the handling of double-quoted strings. In particular, double-quoted strings are much less memory hungry than classic concatenations.

PHP allocates memory at the end of the double-quoted string, making only one call to the allocator. On the other hand, concatenations are allocated each time they include dynamic content, leading to higher memory consumption. Concatenations are still needed with constants, static constants, magic constants, functions, static properties or static methods.

<?php

$bar = 'bar';

/* PHP 7 optimized this */
$a = "foo and $bar";

/* This is PHP 5 code (aka, don't use it) */
$a = 'foo and ' . $bar;

// Constants can't be used with double quotes
$a = 'foo and ' . __DIR__;
$a = "foo and __DIR__"; // __DIR__ is not interpolated

?>

See also PHP 7 performance improvements (3/5): Encapsed strings optimization.

1.2.1336.1. Specs

Short name

Performances/PHP7EncapsedStrings

Rulesets

All, Changed Behavior, Performances

Exakat since

1.0.4

PHP Version

All

Severity

Time To Fix

Precision

Very high

Features

string

Available in

Entreprise Edition, Exakat Cloud