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 4064326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:50:29+00:00 2026-05-20T15:50:29+00:00

I am working on website and I am trying to make it fast as

  • 0

I am working on website and I am trying to make it fast as much as possible – especially the small things that can make my site a little bit quicker.

So, my to my question – I got loop that run 5 times and in each time it echo something, If I’ll make variable and the loop will add the text I want to echo into the variable and just in the end I’ll echo the variable – will it be faster?

loop 1 (with the echo inside the loop)

for ($i = 0;$i < 5;$i++)
{
    echo "test";
}

loop 2 (with the echo outside [when the loop finish])

$echostr = "";
for ($i = 0;$i < 5;$i++)
{
    $echostr .= "test";
}
echo $echostr;

I know that loop 2 will increase a bit the file size and therfore the user will have to download more bytes but If I got huge loop will it be better to use second loop or not?

Thanks.

  • 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-20T15:50:30+00:00Added an answer on May 20, 2026 at 3:50 pm

    The difference is negligible. Do whatever is more readable (which in this case is definitely the first case). The first approach is not a “naive” approach so there will be no major performance difference (it may actually be faster, I’m not sure). The first approach will also use less memory. Also, in many languages (not sure about PHP), appending to strings is expensive, and therefore so is concatenation (because you have to seek to the end of the string, reallocate memory, etc.).

    Moreover, file size does not matter because PHP is entirely server-side — the user never has to download your script (in fact, it would be scary if they did/could). These types of things may matter in Javascript but not in PHP.

    Long story short — don’t write code constantly trying to make micro-optimizations like this. Write the code in the style that is most readable and idiomatic, test to see if performance is good, and if performance is bad then profile and rewrite the sections that perform poorly.

    I’ll end on a quote:

    “premature emphasis on efficiency is a big mistake which may well be the source of most programming complexity and grief.”
    – Donald Knuth

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

Sidebar

Related Questions

I'm trying to make an upload function in this website I'm working on, but
I'm trying to make a multilingual website in Drupal. My languages that I need
I'm working on a website that has a file management portion where users can
I'm trying to make my php mysql driven pages as fast as possible. I've
I am working on my website, and I am trying to get the url
Working on a website that has Employee and Branch entities, using a database table
I'm working on a website that will switch to a new style on a
We are working on a website for a client that (for once) is expected
I'm working on a website that uses lots of png24 files, for transparency. I
I am currently working on a profile for my website. I am trying to

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.