HTML and PHP Code

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
aka_eu
Forum Newbie
Posts: 9
Joined: Wed Sep 28, 2005 10:13 am

HTML and PHP Code

Post by aka_eu »

In one part of my code I have to generate a lot's of html form arranged in huge table. Sometimes I have about 50 lines with 2 forms inside each. I tryed different methods to output the html code but is too slow. From 2 to 4 secconds.

My code is something like this.

Code: Select all

QUERY1 = SELECT 1 made from 3 joined tables  // Time of the execution is very small 
for each line in query1 {
   OUTPUT HTML // A lot of html code with more that 10 php vars inside
   QUERY2 = SELECT 2 made from 2 joined tables // Time of the execution is very very small 
      OUTPUT HTML // SOME html code with more that 3 php vars inside
      for each line in query2{
         OUTPUT HTML// SOME html code with more that 2 php vars inside
     }
  OUTPUT HTML   //closing HTML forms,rows,etc..
}
What is the fastest method to use ?
1. echo "<table ....>$var1.....$var2.....";
2. echo "<table ....>".$var1."......".$var2.".....";
3. ?><table ....><?=$var1;?>.....<?=$var2;?>.....

Or should I use something different ?


If I will put on comments the OUTPUT HTML parts the execution time is less then : 0.05s .
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not test the various methods and find out for yourself and your particular instance?
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

Since you have a query in a loop you are calling that query multiple times. That is going to takes it toll. Also, if your output is large (ie a large table output) it will be slow no matter what. The different methods you mentioned are not going to make any noticable difference.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Might want to turn on output buffering it may help with memory allocation.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Just a quick one,

If you are going to use echo "<table>".$var."</table>" use single quotes (') and not doubles (") as double denotes that PHP should scan the string for variables/special chars, where as single denotes it is to be used literally.
aka_eu
Forum Newbie
Posts: 9
Joined: Wed Sep 28, 2005 10:13 am

Post by aka_eu »

alvinphp wrote:Also, if your output is large (ie a large table output) it will be slow no matter what. The different methods you mentioned are not going to make any noticable difference.
I made my self some tests and I didn't observed any noticable difference. So I'm still looking for a sollution.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

That sounds like a solution to me in itself. If there's not much difference, hooray. :P

Just by guesswork, I would say that either
echo 'word '.$var.' word';
would be faster than
echo "word $var word";

In the first you tell it where the variable is. In the second, it has to find it.

I really don't know how the last one works. Anyone know if <?=$var?> calls the echo function or something?

Anyway,
echo 'word '.$var.' word';
echos text, then a variable, then a word.
word <?=$var?> word
just echos the one variable, so it seems that it might be faster still.

If you constantly switch back and forth between text and variables, though, it might be faster to just use an echo.


But, like I said, this is all pure guesswork. ;)
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Just for some ideas ... Performance
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: HTML and PHP Code

Post by McGruff »

aka_eu wrote:What is the fastest method to use ?
1. echo "<table ....>$var1.....$var2.....";
2. echo "<table ....>".$var1."......".$var2.".....";
3. ?><table ....><?=$var1;?>.....<?=$var2;?>.....
There won't be any measurable difference. If you want to investigate, try apache ab and the Xdebug profiler.

Apache ab is the bottom line. A 10% difference in a chunk of code which only takes up 1% of the total execution time is 10% of nothing.

See RulesOfOptimization
aka_eu
Forum Newbie
Posts: 9
Joined: Wed Sep 28, 2005 10:13 am

Post by aka_eu »

I can say that my code is optimized, and all I have to do.. is to get Zend Optimizer... or to use a Cache system. I'm not sure that the cache system can help me, because the informations are changing very very fast. I have to continue to study the Cache Pear package to see if that can help me. To cache just some parts.
Post Reply