Skip to content

prefer-interpolationfixable

Ensure interpolation is used instead of concatenation

Examples

  • Examples of correct code for this rule using default options

String interpolation

php
<?php
$test = "{$a}{$b}";
$test = "{$a} {$b}";
$a = "foo $(bar7) $1";
$string = "Hello World";
settype($foo , "Bool");

log("Data: {$data}");

// Ignored because it contains non-strings
$this->createError('Error: ' . $error->getMessage('Extra data') . PHP_EOL);
echo inverse(0) . "\n";

// Ignored because it is multiline
echo "This is a very long string that contains
newlines" . "End of string";

// Ignored because it contains double quotes
echo "This is a very long string that contains \"double quotes\"" . "End of string";
  • Examples of incorrect code for this rule using default options

String concatenation

php
<?php
$test = $a . $b;
$test = $a ." ". $b;
$a = "foo \$(bar7)".' $1';
$string = "Hello"." World";
settype($foo , "Bo"."ol");
log("Data: ".$data);

Released under the MIT License.