Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

working on a small PHP project, I've seen in the Internet that it's much better to store all I want to show in a string variable and then show it at once and not echoing all the parts little by little...

i.e. in case of a table with multiple rows, I should echo all the table at once and not echo row by row...

Is that right?

Or it is something an illuminated person thinks?

Currently I don't have a lot of registers to test it... and in my head it seems it's worse as I've got the array of data, storing everything in a string to show it later seems a memory hog to me...

What I have tried:

nothing it's a simple question of what is better,...
Posted
Updated 2-Sep-19 1:31am
Comments
Richard MacCutchan 1-Sep-19 3:22am    
If you want to display a table then you should use HTML, which has built in support for displaying data in such a format.
Afzaal Ahmad Zeeshan 1-Sep-19 4:52am    
Read the output line-by-line and generate an HTML table that can be rendered. Does that make sense?
Joan M 1-Sep-19 6:28am    
Read my other comment please.
Joan M 1-Sep-19 4:52am    
Of course! I've explained myself very badly...

I meant it is better to do:

echo(' my value ');
echo(' my value2 ');
echo(' my value3 ');
echo(' my value4 ');
...

or

$stringwithall .= ' my value ';
$stringwithall .= ' my value2 ';
$stringwithall .= ' my value3 ';
$stringwithall .= ' my value4 ';
...
echo($stringwithall);

I think the second method consumes more memory and gives no better results...
Afzaal Ahmad Zeeshan 1-Sep-19 13:11pm    
You do not need to do echo, just concatenate the string and print it once.

1 solution

Quote:
I've seen in the Internet that it's much better to store all I want to show in a string variable and then show it at once and not echoing all the parts little by little...

There is many aspects.
I don't see a point to build a very large string and echo it at once.
If I build a table, I would echo once per row.

Strings:
Strings directly a chink of continuous memory, this mean that every time you happen a little chunk of string to a bigger one, it involve a copy of both parts, the problem is that it degenerate very quickly.
Example: you must concatenate 100 little strings (same size), it will involve copying 5050 the size of a little string.

String builder:
This object is conceived to avoid endless copy of previous part of string, thus having a vastly better runtime.

Echo/write to file
is tailored to handle small chunks of writing, so it will not particularly worse than 1 single huge write.
The advantage of small chunks is to avoid huge memory hog.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900