Strings

Prefer string interpolation above sprintf and the concatenation . operator whenever possible. Always wrap the variables in curly-braces {} when using interpolation.

PHP
// GOOD
$greeting = "Hi, I am {$name}.";

// BAD (hard to distinguish the variable)
$greeting = "Hi, I am $name.";
// BAD (less readable)
$greeting = 'Hi, I am '.$name.'.';

sprintf

For more complex cases when there are a lot of variable to concat or when it’s not possible to use string interpolation, please use sprintf function or have a look at the laravel Str minipulation objects which can be chained:

PHP

PHP
$greeting = sprintf('Hello, my name is %s', ucfirst(auth()->user()->name));

Laravel

PHP
use Illuminate\Support\Str;
 
$string = 'Laravel 10.x';
 
$replaced = Str::replace('10.x', '11.x', $string);

// Laravel 11.x