Php

Strings

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

// 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, where there are a lot of variables to concatenate or where string interpolation is not possible, please use the sprintf function, or have a look at Laravel's Str manipulation helpers, which can be chained:

PHP

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

Laravel

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

Laravel Fluent string

A better option is to stick to the Laravel defaults and use their great fluent string helpers and classes.

Copyright © 2026