Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6215783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:07:03+00:00 2026-05-24T07:07:03+00:00

I do lots of string concatenation in code and then I display output. I

  • 0

I do lots of string concatenation in code and then I display output. I was wondering if there is any difference between following two codes:

string concat

$str = '';
for($x =0; $x <= 10000; $x++):
    $str .= 'I am string '. $x . "\n";
endforeach;

Output buffering

 ob_start();
 for($x =0; $x <= 10000; $x++):
    echo 'I am string ', $x , "\n";
 endforeach;
 $str = ob_get_contents();
 ob_end_flush();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:07:05+00:00Added an answer on May 24, 2026 at 7:07 am

    Here’s a benchmark for you:

    <?php
    
    $test_1_start = microtime();
    
    $str = '';
    for ( $x = 0; $x <= 10000; $x++ ) {
        $str .= 'I am string ' . $x . "\n";
    }
    
    $test_1_end = microtime();
    unset($str);
    echo 'String concatenation: ' . ( $test_1_end - $test_1_start ) . ' seconds';
    
    $test_2_start = microtime();
    
    ob_start();
    for ( $x = 0; $x <= 10000; $x++ ) {
        echo 'I am string ', $x, "\n";
    }
    $str = ob_get_contents();
    ob_end_clean();
    
    $test_2_end = microtime();
    
    echo "\nOutput buffering: " . ( $test_2_end - $test_2_start ) . ' seconds';
    
    ?>
    

    My results:

    $ php -v
    PHP 5.3.4 (cli) (built: Dec 15 2010 12:15:07) 
    Copyright (c) 1997-2010 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    $ php test.php
    String concatenation: 0.003932 seconds
    Output buffering: 0.002841 seconds%
    $ php test.php
    String concatenation: 0.004179 seconds
    Output buffering: 0.002796 seconds%
    $ php test.php
    String concatenation: 0.006768 seconds
    Output buffering: 0.002849 seconds%
    $ php test.php
    String concatenation: 0.004925 seconds
    Output buffering: 0.002764 seconds%
    $ php test.php
    String concatenation: 0.004066 seconds
    Output buffering: 0.002792 seconds%
    $ php test.php
    String concatenation: 0.004049 seconds
    Output buffering: 0.002837 seconds%
    

    Looks like output buffering + echo is consistently faster, at least in the CLI / on my machine.

    Brendan Long made a good point in his comment, however — there is such a minor performance difference here that a choice of one or the other is not much of an issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are lots of scripts for detecting the User Agent string to detect iPhones
I have lots of source code files. I have lots of hard coded string.
In PHP, I'm frequently doing lots of string manipulation. Is it alright to split
I'm trying to parse this XML string: <?xml version=1.0 encoding=UTF-8 standalone=no?> <response type=success> <lots>
Lots of people talk about writing tests for their code before they start writing
Lots of frameworks let me expose an ejb as a webservice. But then 2
just wondering what's the best approach for template String replace. preg_replace('/{%(\S+)%}/', 'bill', $tableOutput); With
I have a string containing lots of text with white-spaces like: String str =
Any code can provide side effects. Most of the time, side effects can be
I have a string lots\t of\nwhitespace\r\n which I have simplified but I still need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.