Jump to content
dalawh

Which is more efficient?

Recommended Posts

When I am coding in a php file (.php extension), is it better to reduce the php area to as small as possible (like in the first case) or does it not matter (like the second case)?

<html><body><?php//Some php code?></body></html>

OR

<?php<html><body>//Some php code</body></html>?>

Share this post


Link to post
Share on other sites
Guest So Called

The second example does not work. You need to echo "<html><body>..." within <?php ?> Even if you use echo I doubt it would make any significant difference.

Edited by So Called

Share this post


Link to post
Share on other sites

There's no reason to have PHP output static HTML code, PHP is for creating dynamic content. You'll find that your pages are much easier to maintain if the PHP and HTML are separate.

Share this post


Link to post
Share on other sites
The second example does not work. You need to echo "<html><body>..." within <?php ?> Even if you use echo I doubt it would make any significant difference.
Sorry about that. I just typed it quickly. Oh okay. Just wanted to make sure. It doesn't make a difference even if the file gets quite big?

Share this post


Link to post
Share on other sites
Guest So Called
There's no reason to have PHP output static HTML code, PHP is for creating dynamic content. You'll find that your pages are much easier to maintain if the PHP and HTML are separate.
That is just your opinion and is not an absolute. For me it depends. If there's a whole lot of HTML and a little bit of PHP then I'll probably let it be static, using <?php ... ?> to insert PHP snippets. But if it's a whole bunch of PHP and not that much HTML then I'll use echo "..." to echo HTML snippets. My pages tend to be mostly PHP and most of my content (HTML) resides in a MySQL database. Even static content.

Share this post


Link to post
Share on other sites
That is just your opinion and is not an absolute.
That's true, it's my opinion based on several years of professional experience. I certainly didn't start out with a clean separation of PHP and HTML, that has evolved over time as I've learned about what kinds of projects are easier to maintain. As it is, common accepted best practice is to do all of your PHP processing at the start, before outputting any HTML at all. Any PHP code in the HTML parts should only output variables that were calculated earlier, plus whatever control structures are required. This commonly leads to use of a template engine, where you calculate all data for the page, call up your template, and send the data to the template engine to have that create the HTML output. In that case you have no HTML code in your PHP files at all, all of the HTML code is in external templates. Designers can edit the templates, and programmers can edit the PHP code, in separate files. You can also use a completely different template with the same data to get an entirely new HTML structure without changing the PHP code. But, to each his own. I'm not trying to dictate anything, like you said I'm only giving my opinion. I stand by my assertion that there is no reason to invoke PHP just to output a string of static HTML code, there's no reason to use the overhead of the PHP engine. This:
echo '<html><head><title>Test Page</title></head>';echo '<body>' . date('r') . '</body>';echo '</html>';

Is demonstrably less efficient than this:

<html><head><title>Test Page</title></head><body><?php echo date('r'); ?></body></html>

Share this post


Link to post
Share on other sites
Guest So Called

I told you it depends on how much PHP code is involved vs how much HTML code, yet you gave an example with far more HTML than PHP, which is consistent with what I said.

Share this post


Link to post
Share on other sites
Guest So Called

BTW I would have coded your first example like this:

echo '<html><head><title>Test Page</title></head><body>' . date('r') . '</body></html>';

It appears that you made your "not this way" example more inefficient than it need be.

Share this post


Link to post
Share on other sites
I told you it depends on how much PHP code is involved vs how much HTML code, yet you gave an example with far more HTML than PHP, which is consistent with what I said.
I think you are missing the point

Share this post


Link to post
Share on other sites
Guest So Called

I think you are missing the point. You set up a straw man and then knocked it down and you think you've proved something. My rewriting of your "wrong" example shows that your "right" example differs only in saving a few apostrophes and periods. There is no significant difference between the two. If you want to prove something you should come up with better examples. My earlier post said that if there is more HTML than PHP then it would be better to leave the file in HTML mode and break into PHP for inserts, exactly what your examples show.

Share this post


Link to post
Share on other sites

I get what jsg and sc are trying to say. I personally like sc methods because it is much simplier, but jsg does make sense. Thanks for your input.

Share this post


Link to post
Share on other sites
I think you are missing the point. You set up a straw man and then knocked it down and you think you've proved something. My rewriting of your "wrong" example shows that your "right" example differs only in saving a few apostrophes and periods. There is no significant difference between the two. If you want to prove something you should come up with better examples.
No, you're definitely missing the point (and there's no reason to shoot astralaaron over this). The point is that there is no reason to invoke the PHP engine to output HTML that does not change. Period. Full stop. End of discussion. There's literally no point, it's just asking the server to do more work than is required. The ratio of HTML to PHP is not relevant. If the question is about efficiency, then it is more efficient to have the server do less work for the same result.
There is no significant difference between the two.
There's where you miss the point - the significant difference is that in one case you invoke PHP to output a piece of text that won't change, and in the other situation you let that pass straight through and only invoke PHP for what you need it to do. Again, the amount of HTML content vs. PHP content in the deliberately simple example I posted is not relevant to this point. And it's terribly inefficient to write your entire website one way, then when the ratio of HTML to PHP changes are you going to go through and rewrite it? Why not start with the most efficient way? Where is this ratio limit exactly? Either take the time to do it right, or make the time to do it over.

Share this post


Link to post
Share on other sites
Guest So Called

In practice it makes no significant difference. Often the best choice is to pick the most easy to read method to write code, because it's easier to write, easier to debug, and easier to maintain. I have no pages that start out <?php <html><head>....." All my pages start out in PHP because stuff like <html><head> is in my MySQL database. Out of 80-100 pages they all start out exactly the same way, so why have <html><head> at the beginning of every PHP script? I have a function get_header() which can be as simple as echo get_header() or can be $variable = get_header() and then use str_replace() to adjust for things like META description/keywords. Your examples are reductions to triviality. In the real world pages and scripts are far more complex than that. In the real world it does not matter if you save 50 microseconds of PHP execution time. What matters in the real world is ease of maintenance. Content changes often, code relatively seldom. It makes no sense to mix content into PHP scripts any more than necessary.

Share this post


Link to post
Share on other sites
Your examples are reductions to triviality.
So were the examples posted by the OP. Again, that is not the point. He posted a trivial example and asked which style is more efficient. There's only one answer to that question. The first style is more efficient. How much more efficient? Not much. But it's more efficient.
In the real world it does not matter if you save 50 microseconds of PHP execution time. What matters in the real world is ease of maintenance.
In the real world they both matter. I've got servers handling a total of 12-15 requests per second that deliver almost 2TB of content every month, you better believe it that 50 microseconds matters. If I can shave 50 microseconds off of a process that happens tens of thousands of times per day then that is not insignificant. There are processes running on that and another server which literally can take 10 hours or more to finish, small efficiencies lead to very noticeable gains. But that still isn't the point - the point is that one way is more efficient, regardless of how more efficient it is, and that there is literally no reason to do it the other way. It doesn't make the code easier to read, and it doesn't make the code faster. The fact is that this:
<?phpecho 'some static content';...?>

is less efficient than this:

some static content<?php...?>

That's what the OP was asking, and that's the answer. It's no more complicated than that, there's no reason to have an argument about it.

Share this post


Link to post
Share on other sites

If you're planning to allow other developers to design layouts and templates with your custom content management system it is better to allow them to work with it as ordinary HTML rather than using PHP to print out the code.

Share this post


Link to post
Share on other sites
All my pages start out in PHP because stuff like <html><head> is in my MySQL database
as you are pulling everything from the database so it might be relevant to think that echoing the whole page inside php block is the way and it is if you pulling just a everything from it. but there is better way of doing this. people usualy dont store page markups in their database. they store its contents. those contents are being inject into html markup. that is where template engine comes. you may heared of MVC which is based on same theory, seprating the markup from the php code. You definitly seeing the problem from one point of view but if you consider other scenerio you will find it is always easier to maintain separated markup and codes. Efficiency is an issue and also the maintainability. i felt the importance of the second one more. writing markup with php may not hurt but in practical larger implementetion it is not suitable.

Share this post


Link to post
Share on other sites
Guest So Called

My content is stored separate from other parts of the pages, for example common header (starts with <!DOCTYPE...><HTML>...) and common footer (ends with </BODY></HTML>). I have various other parts of pages used in some of my pages. Look at WordPress. My site works exactly like that. PHP front end, MySQL back end. If you want to edit your content you login to an admin account and use an editor. Separating code from content allows the code to be maintained separately by software people, and content to be managed by content creators. The code can be reused on similar sites, and WP is a great example of that, perhaps 100s of thousands or millions of blogs running the same code (perhaps different versions/releases of same code). WP is an excellent example of why you'd want to keep your content out of your code.

Share this post


Link to post
Share on other sites
Separating code from content allows the code to be maintained separately by software people, and content to be managed by content creators. The code can be reused on similar sites, and WP is a great example of that, perhaps 100s of thousands or millions of blogs running the same code (perhaps different versions/releases of same code). WP is an excellent example of why you'd want to keep your content out of your code.
Right, that's why I suggested using a template engine in post 6, and why Ingolme and birbal also mentioned templates. I wouldn't necessarily hold up WordPress as an example of good programming practice, but whatever. If you want to look into a popular template system, Smarty is used by a lot of people. http://www.smarty.net/

Share this post


Link to post
Share on other sites
Guest So Called

I meant my code is based upon the same concepts as WordPress and other CDS, HTML front end and MySQL back end. I did not mean it as an endorsement of WordPress's particular coding style, although recognizing that they are a large co-operative project with programming contributors of widely varying skill levels. My own CDS has no commercial potential, it's just a hobby, and eventually I'll tire of it and probably switch to WordPress and port my best ideas into WordPress plug-ins, and offer them as donation-ware and get a small income from my hobby. My choice of WordPress is not really any endorsement but rather just an appraisal that there are so many WordPress users that they would be the largest pool of users who could support my plug-ins. I'm pretty sure I'll reach the day when I tire of continued development and maintenance of a one-of-a-kind CDS. WordPress does an excellent job of presenting content to site visitors who could care less what the code looks like.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...