---
title: "Strings"
canonical_url: "https://handbook.propay.co.za/php/strings"
---
# Strings

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

<code-group>

```php [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.'.';
```

</code-group>

## 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](https://laravel.com/docs/12.x/strings), which can be chained:

PHP

<code-group>

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

</code-group>

Laravel

<code-group>

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

</code-group>

## Laravel Fluent string

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

- [Laravel Strings documentation](https://laravel.com/docs/12.x/strings)


## Sitemap

See the full [sitemap](https://handbook.propay.co.za/sitemap.md) for all pages.
