If I’m using PHP for the logic in my site but it also has a lot of html in it which is smarter (efficiency) to echo my html or to end php ?> and then begin it again when i’m done with html <?php
That is a great question.
First, you must always remember that PHP is SERVER-SIDE only code. HTML, Javascript, JQuery and AJAX (among others) are all BROWSER or CLIENT-SIDE. Your PHP code is handled on the server before your browser even sees the page at all.
Next, the output from the PHP code is then posted our to the browser. Inside the browser, the HTML is put in place, then the CSS is added to the page and finally the JS/JQuery/etc is executed to finish it off.
Therefore, any PHP coding that you wish done should be first. The HTML is part of the text file that gets posted to the browser. This means that if you add a PHP line such as " echo ‘sometext’; " it is converted by the PHP processor into " sometext ". Then, when sent to the browser, the browser handled whatever this text was. Therefore, the “sometext” actually is processed twice. Therefore many programmers suggest that you do not do this and only use the PHP to actually alter the HTML as needed.
Now, this brings us to the biggest part of this question… What does your site do? If it is a full fledged “Blog” which would have many hits per second on it, then, you would want to save as much overhead as possible and place your HTML code in the pages AS HTML code and not extra processing needed for PHP ECHO’s commands.
But, if your site is a simple photo page just for your friends who look at the pixes once a week, then it really doesn’t matter as the server’s overhead would be low. This really depends on the type of site you are creating and it’s uses and demands.
Another example, I have a site that is for my buddy’s football pool. There is less than 100 active on the site. They basically just go in each week and select their picks. Such a small server “load” that any type of programming is just fine. I am working on another high-level site that will eventually have thousands of members and tons of video streaming coming thru the server. The load on that site will be extremely high. Therefore, I need to keep the PHP programming small to keep the server load as low as possible.
One further note. On the browser side, the overhead is not as important as it is running in the client’s computer, not your server. So, keep that in mind.
Recapping, PHP is SERVER-SIDE and most of the rest is all CLIENT-SIDE. If you have a large number of members or clients hitting on the server, try your best to keep that side with smaller and tighter code.
Hope that info helps answer your question. Good luck…
The short answer would be to end PHP as the HTML code doesn’t need to go through the PHP parser thus saving a little time and server resources.
And yep I agree, great question, and a little insight into your thought process.
Red
Ah thank you so much for your detailed response it makes perfect sense! You’re awesome
You are welcome. I do find that new members here and around the world along with programmers new to ASP or PHP or other server-side programming do not grasp the SERVER-SIDE and CLIENT-SIDE differences. Glad you understand it. I will mark this one solved. If you have another question , please start a new post! Thanks!
Happy programming…