Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Making string concatenation readable in PHP

Probably all PHP developers know how to concatenate strings. The most popular method is using the .-operator.  For small concatenations using this operator works fine. When lots of strings or variables need to be combined it can become cumbersome.

Here’s an example:

$logMessage = 'A '.$user->type.' with e-mailaddress '.$user->email.' has performed '.$action.' on '.$subject.'.';

Wow, my fingers hurt… While typing the most time is spent on making sure the quotes are open and closed on the right places. It also isn’t very readable. Especially the .'.' at the end make my eyes bleed.

A better way to create the string is to use the sprintf-function.
This example produces the same string as the code above:

$logMessage = sprintf('A %s with email %s has performed %s on %s.', $user->type, $user->email, $action, $subject);

That’s much better. This is much easier to type and you don’t have to pay attention to opening an closing quotes. But my eyes still need to do a lot of work. To find out which string goes in which %s-placeholder you have to switch watching the beginning and end of the line of code.

There is another option to concatenate strings. It uses curly braces. Let’s take a look:

$logMessage = "A {$user->type} with e-mailaddress {$user->email} has performed {$action} on {$subject}."

In my mind this is much better. This option has the least amount of characters to type. Your eyes can just read the line of code from left to right to understand what’s going on.
Keep in mind that this only works when using the curly braces between a double quote. The first character after the opening curly braces should be a dollar-sign.

EDIT: The curly braces syntax works well if you only need to concatenate strings. As mentioned in the comments below this post, sprintf might be a better fit when concatenating some other type of variable.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • Juukie14

    Sprintf allows you to treat values as a float or integer for example. Use %f / %d instead of %s. It also allows you to place one value on more places, the syntax will be a bit different. Checkout http://php.net/manual/en/function.sprintf.php for details.
    Also note that {$param} won’t work when between single quotes.

    • Yeah, when using numeric types that need formatting sprintf might be a better option. Notice that I handpicked my example to use only strings 🙂

      • Juukie14

        I did notice but wanted to show what could make sprintf a better one to use. ?

  • Xu Ding

    Agree. We have been using sprintf to make it more readable. Another benefit is l10n.


  • Dmitry Krokhin

    In your case you simply use:
    $text = "A $user->type with $user->email has performed $action on $subject.";

  • Pingback: Making string concatination readable in JavaScript | murze.be()

  • Pingback: ? This blog is one year old ? | murze.be()