Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: One big echo (or print) VS many small echo (or print)

I should echo a table with 1000 rows of data, can I echo all the data in one string or is it better to echo a row per time?

like image 207
vitto Avatar asked Dec 17 '10 13:12

vitto


People also ask

Which is faster echo or print in PHP?

They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print .

Why is preferable over with Echo print in PHP?

Echo vs Print Echo can be used in expressions as it has no return value while print has a return value of 1. echo is marginally faster than print and can take multiple parameters while print can take only one argument.

Where does PHP echo go?

php echo directly in the browser. If you directly view backend. php in your browser you will see the echo in the same way as index. php.


1 Answers

According to http://phplens.com/lens/php-book/optimizing-debugging-php.php (see last third of article) you should use one big echo statement, and use single quotes instead of double quotes, so PHP hasn´t to check for variables within the string.

simple test for support:

$bgn = microtime(true);
for ($i=0; $i<=1000; $i++)
{
  echo $i;
}
echo "\n", round(microtime(true)-$bgn, 4), "\n";
unset($bgn);
$bgn = microtime(true);
$b = '';
for ($i=0; $i<=1000; $i++)
{
  $b.=$i;
}
echo $b;
echo "\n", round(microtime(true)-$bgn, 4), "\n";
?>

First run return 0.0022, while second run return 0.0007 ... however this is small set of data, memory usage is quite small.

like image 122
Tobias Avatar answered Sep 21 '22 01:09

Tobias