How you write your String matters: benchmarking PHP

Andrea Olivato
2 min readJul 9, 2021


There are a lot of ways in which you can specify, use and print string in PHP. They all have different benefits and drawbacks and that’s not the content of this article. The focus of these tests is to determine how using different string specifications affects the performances of your PHP code.

TL;DR: Single quoted strings are the most performing, both for simple text and for concatenation with other variables.


Compared 3 types of string specification:

  • Single Quoted (e.g. echo 'string';)
  • Double Quoted (e.g. echo "string";)
  • HEREDOC (e.g. echo <<<EOT string EOT;)

For each type of specification, 2 tests:

  • Simple string (e.g. 'string')
  • String + a variable (e.g. 'string'.$var)


In order to simulate a strong workload and have more data, the code ran 10 iterations with a 5-second pause. For each iteration 1 Million echo were executed, without any pause.

To make sure that the machine load doesn’t skew the results, all the 3 different specifications are executed within the same iteration, one after the other, with the same 5-second delay. So at any point in time, the load should be equally distributed among the different specifications.


Since the actual printing of the strings must not affect these tests, ob_start() and ob_end_clean() are used to not print anything, and ob_clean() is used within each iteration to free up the buffer.

The Code

This is the code used to run the tests.

Summary Results

Unsurprisingly, the Single Quoted strings are the fastest: as they don’t require parsing for variables or special characters (e.g. \n), they run quicker than the others both for simple strings and for variables.

  • Simple String: 0.3838947773 seconds for 1 Million executions
  • String+Variable: 0.532834816 seconds for 1 Million executions

Heredoc performs a bit slower than Single Quoted strings, but faster than Double Quoted ones, especially when without variables.

  • Simple String: 0.387871933 seconds for 1 Million executions
  • String+Variable: 0.553892827 seconds for 1 Million executions

Finally Double Quoted strings:

  • Simple String: 0.3914105415 seconds for 1 Million executions
  • String+Variable: 0.5623492718 seconds for 1 Million executions

Complete Results

Below you can see a sneak peak of the final results. If you click on the image or here , you can access a shared spreadsheet with the complete data.


  • PHP 7.4.19
  • Amazon Linux 2 AMI
  • Kernel 4.14.232–177.418.amzn2.x86_64
  • 16GB Memory
  • 4 Virtual CPUs



Andrea Olivato

Co-founder @Lnk.Bio — Developing stuff on the Web since 2003