Hi, Does anyone know how much of an overhead on a web server's processing power is it to frequently shift in and out of <?php [...] ?> I've got a dynamically generated page that's a bit slow and I'm wondering if I recode it to avoid transitioning in and out of HTML, whether this is likely to make a siginificant difference. Thanks.
I'm not sure I understand. What is returned to the browser is all HTML and is what PHP sends to the browser. If you mean having bits of PHP code throughout your document I doubt this is your problem. What does this page do? Does this slow page access MYSQL?
Yes, it accesses MySQL. Let me give 2 examples: 1. Switching between PHP and HTML <table><tr><td> <?php echo $x; ?> </td><td> <?php echo $y; ?> </td></tr></table> 2. Staying in PHP <?php echo '<table><tr><td>'.$x.'</td><td>'.$y.'</td></tr></table>'; ?> Would one of these be faster than the other? Thanks.
I've assumed the latter is the better for all coding. Mostly because this: BEGIN PHP ECHO HTML END PHP VS. BEGIN HTML END HTML BEGIN PHP END PHP BEGIN HTML END HTML
Okay, thanks for the advice guys. The slowness in the page is probably because of frequent SQL queries, sometimes hundreds in one page, but if modifying the way the PHP is written will speed it up at all, I'll give it a go. Cheers.
Mate, keep your PHP / HTML readable (first form). This will hardly have any effect at all. It's probably immeasureable.. As you said, slowness is due to other causes such as DB.
Nobody needs that kind of queries. You are probably able to reduce this amount by more efficient coding and sql usage. Otherwise, you need to change your database structure.
Agreed, compared to other factors like database access this one would have an absolutely negligible effect on speed of execution. You will see big-league PHP code that switches in an out of PHP a huge amount like this. Although technically it may be quicker to echo HTML from PHP than to switch out of PHP, the latter way is a lot neater in my opinion, than echoing it, and this may also be considered the better practice since you are maintaining a separation between code and output. I am no expert, but there are ways and means to significantly optimise SQL code - do you really need multiple queries where one could do the job? Are the fields you are searching/sorting on indexed? Are you importing large amounts of data into the dbase which could be imported using LOAD instead of INSERT..? I'm sure there are plenty more things you can try too.