How you write your String matters: benchmarking PHP

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




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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Elder Scrolls V: Skyrim Special Edition — Analysis of Dialogues

Instructions for Use 3 Best Android Emulators on Windows 10

Is JS and Python justified in their popularity?

CS 160 Final Project: Build-a-Beat

Responsive | Dynamic | Web Friendly Images, Conversion, and Generation with C#

How Programming is different today?

Quick-guide on Software Pre-Cert Program (1) - Freiheit als Autonomie as an essence

C Sharp

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrea Olivato

Andrea Olivato

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

More from Medium

Show a CMS Page in frontend through XML-Magento 2

Testing Twig Extensions The Right Way

A handsome, young Afro-American  man pointing on his temple thinking: “no tests? More time for coding”

How to use Laravel’s default user table for Twill CMS authentication.

PHP and Node.js are used For an ERP system