Jump to content

[SOLVED] Quick PHP question


Brenden Frank

Recommended Posts

Which is the best method or does it even matter? I know this example isn't very practical but I've seen times where I'd like to simply store all my outputs as a string then do one big final output at the end. However I could see if this string is getting big that this could cause overhead. Or would echo do about the same?

 

$output = "";

for ($i=0;$i<50;$i++) //loop and create output string
{
   $output .= "Something"; //gather everything into a string
}
echo $output; //output string

 

or

 

for ($i=0;$i<50;$i++) //loop and create output string
{
   echo "Something"; //output the string as it comes
}

 

I like the output method because it allows me to very specifically monitor what is being outputted and is of course the most useful for functions with return values.

Link to comment
Share on other sites

<?php
$output = "";

for ($i=0;$i<10000;$i++) //loop and create output string
{
   $output .= "Something"; //gather everything into a string
}
echo $output; //output string

this code took 10.362ms to run on avarage

 

or

 

<?php
for ($i=0;$i<10000;$i++) //loop and create output string
{
   echo "Something"; //output the string as it comes
}

this code took 11.45ms seconds to run on avarage

 

used Zend Profiler for both...:-) this is probably a better comparison,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.