Brenden Frank Posted January 1, 2009 Share Posted January 1, 2009 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 https://forums.phpfreaks.com/topic/139111-solved-quick-php-question/ Share on other sites More sharing options...
DarkSuperHero Posted January 1, 2009 Share Posted January 1, 2009 I like to put it into a variable if I'm doing something else to it, like double checking its contents or formatting.. using Zend profiles, both took about the same to run...averaging out at .12ms... Link to comment https://forums.phpfreaks.com/topic/139111-solved-quick-php-question/#findComment-727565 Share on other sites More sharing options...
Brenden Frank Posted January 1, 2009 Author Share Posted January 1, 2009 Perfect, that's all I needed to know. Thank you very much. Link to comment https://forums.phpfreaks.com/topic/139111-solved-quick-php-question/#findComment-727568 Share on other sites More sharing options...
DarkSuperHero Posted January 1, 2009 Share Posted January 1, 2009 <?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 https://forums.phpfreaks.com/topic/139111-solved-quick-php-question/#findComment-727583 Share on other sites More sharing options...
Brenden Frank Posted January 1, 2009 Author Share Posted January 1, 2009 Ah, perfect. It's even faster. Link to comment https://forums.phpfreaks.com/topic/139111-solved-quick-php-question/#findComment-727586 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.