Multiline Strings in PHP

Multiline Strings in PHP
Photo by Andraz Lazic / Unsplash

A common task I face when programming is to store multi-line strings in a variable. This may be related to templating for views, emails or configuration files for third party systems, or just writing readable inputs for tests.

The naive approach

First I want to show you the naive approach:

function makeUserJson(): string
{
    return "
      {
        \"name\": \"John Doe\",
        \"email\": \"john@example.org\",
        \"timezone\": \"Europe/Paris\"
      }
    ";
}

echo "start" . PHP_EOL;
echo makeUserJson() . PHP_EOL;
echo "end" . PHP_EOL;

The resulting literal output (notice the extra blank lines and indentation):

start

      {
        "name": "John Doe",
        "email": "john@example.org",
        "timezone": "Europe/Paris"
      }
    
end

Problems in the output:

  • there are additional blank lines above and below the json object
  • the level of indentation of the result depends on the level of indentation in your source code
  • the extra whitespace may lead to unexpected behavior when further processing the string
  • we have to escape quotes with backslashes

Using Heredoc

Here's how you can fix all of the above problems with Heredoc:

function makeUserJson2(): string
{
    return <<<JSON
      {
        "name": "John Doe",
        "email": "john@example.org",
        "timezone": "Europe/Paris"
      }
      JSON;
}

echo "start" . PHP_EOL;
echo makeUserJson2() . PHP_EOL;
echo "end" . PHP_EOL;

The result:

start
{
  "name": "John Doe",
  "email": "john@example.org",
  "timezone": "Europe/Paris"
}
end
  • additional whitespace on the left is removed and no longer depends on its position in the source code
  • no extra blank lines are added
  • no need to escape quotes
💡
Note the end marker in line 9 of the makeUserJson2() function. It must be on the same indentation level as the rest of the string, in order to cut off all the superfluous whitespace left and above of it.

Variable Support

You can use variables inside your multi-line strings:

function makeUserJson3(string $name): string
{
    return <<<JSON
      {
        "name": "$name",
        "email": "jane@example.org",
        "timezone": "Europe/Paris"
      }
      JSON;
}

echo makeUserJson3("Jane White");

In the result, $name is replaced with the concrete value:

{
  "name": "Jane White",
  "email": "jane@example.org",
  "timezone": "Europe/Paris"
}

Disabling Interpolation with Nowdoc

If you do not want the string to be interpolated, use Nowdoc instead, by adding single quotes to the start marker:

function makeUserJson3(): string
{
    return <<<'JSON'
      {
        "name": "$name",
        "email": "jane@example.org",
        "timezone": "Europe/Paris"
      }
      JSON;
}

echo makeUserJson3();

In the result, $name is written out literally, because we disabled interpolation:

{
  "name": "$name",
  "email": "jane@example.org",
  "timezone": "Europe/Paris"
}

Summary

In this blog post, you learned how to create multi-line strings in PHP, without the need for worrying about escaping quotes or indentation of source code, with variable interpolation (Heredoc) or without (Nowdoc).