Forum Moderators: coopster

Message Too Old, No Replies

To echo or not to echo

HTML inside or outside PHP?

         

Maynard

4:02 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



Which is faster for the server to process:

HTML inside echo tags or outside of PHP altogether?

Maynard.

coopster

7:27 pm on Mar 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



...for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print() or somesuch.

Escaping from HTML [php.net]

mbcx9rvt

1:45 am on Mar 19, 2004 (gmt 0)

10+ Year Member



<<<END is sometimes used. The difference between escaping and not escaping out of PHP is tiny! Only if your page is *huge* will you notice even then slightest difference. Some say it is neater to stay in PHP, others disagree.

I am in the camp of escpaing out of PHP, simply because I think it reads easier!

HTH

mbcx9rvt

Gorilla

2:31 am on Mar 19, 2004 (gmt 0)

10+ Year Member



Somewhat related: Having a large number of echo-statements (100s to 1000s) per page can consume measurable amounts of processing time. I recently optimised a PHP site for speed and found that accumulating all output for a page into a variable before echoing everything at once was well worth doing.

bobnew32

4:17 am on Mar 19, 2004 (gmt 0)

10+ Year Member



Or just use smarty templating system like I do. Very simple and easy to use. :)

Maynard

9:41 am on Mar 19, 2004 (gmt 0)

10+ Year Member



Thanks. I prefer writing my HTML outside of PHP tags so that I don't have to keep escaping certain characters. And yes, I agree, it makes it easier to read when troubleshooting or finetuning.

jatar_k

6:51 pm on Mar 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



another point about echo'ing, don't use concatenation operators in your echo statements

$who = " we ";
echo "this is how" . $who . "echo strings.";

now in the above example the various parts need to be cat'ed together before they can be echo'ed

$who = " we ";
echo "this is how",$who,"echo strings.";

the above just fires em off.

ergophobe

5:06 pm on Mar 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



jatar_k,

I'll grant that the difference is infintesimal, but when i benchmark the two methods, concatenation is somewhat faster than substitution (and that's what the original post asked about).

Of course, the differences in speed are tiny, so readability should rule.

The only output method that I've benchmarked to be really really slow is the heredoc syntax. To get big differences between escaping and not escaping from PHP, you need to have either many very small or some very large text chunks.

Tom

ergophobe

5:41 pm on Mar 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since it's quite a screenful, I've hesitated to post my text benchmarking [webmasterworld.com], but since it keeps coming up, I did. You can see it in this thread [webmasterworld.com].

Hester

2:41 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On a related note, I've a question. I've replaced some repetitive code on my site with calls to PHP, to include a common image link used on many pages. So now the HTML may make several calls to PHP. But I could also scan the page and do a replace. Now there are 3 types of image links to replace, not just one. So which method is the best?

Example 1 (current method used):

PHP in header:
$excel = " <img src=\"images/excel.gif\" width=\"14\" height=\"14\" alt=\"Excel logo\" title=\"Excel\" />";
$word = " <img src=\"images/word.gif\" width=\"14\" height=\"14\" alt=\"Word logo\" title=\"Word\" />";
$pdf = " <img src=\"images/pdf.gif\" width=\"14\" height=\"14\" alt=\"PDF logo\" title=\"PDF\" />";

HTML:
<a href="wordfile.doc">Word File</a><?php echo $word;?>
<a href="excelfile.xls">Excel File</a><?php echo $excel;?>
<a href="pdffile.pdf">PDF File</a><?php echo $pdf;?>

Example 2
Put page into variable (requiring every quote mark to be escaped! Or converted to a single quote mark, not a double one). Scan it three times and replace each instance of a custom symbol to give the image link code. Eg:

HTML:
<a href="wordfile.doc">Word File</a>*W*
<a href="excelfile.xls">Excel File</a>*X*
<a href="pdffile.pdf">PDF File</a>*P*

Where *W*, *X* and *P* would be the code to replace. (Of course you'd have to make sure no-one used those exact characters elsewhere on the page.)

The second example seems more complicated, but which is better in terms of speed and avoiding hammering the PHP parser?

(There is a third way - scan the page for the document extension and add the code when required - but I don't know how to do that - it would mean scanning for the end of the link tag as well.)

Hester

3:20 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


I'm now doing both! A certain set of pages benefit from the second approach, which I've made happen with just one replace, by using an array like this:

$trans2 = array("[word]" => $word, "[excel]" => $excel, "[pdf]" => $pdf);

So I just add the relevant type at the end of each link, then output the replaced result like this:

$contents = strtr($contents, $trans2);
print $contents;

Nova Reticulis

4:01 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



I would strongly recommend to never mix PHP and HTML code in any serious production projects and resort to templating systems such as Smarty [smarty.php.net] which I use everywhere I can and it does save me a lot of time, especially with its plugins and caching.

ergophobe

4:11 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




never mix PHP and HTML code

Ultimately, if it's a web page, you are mixing them somewhere, be it within your template or not.

That said, Hester, I was just about to say myself that it sounds like your project is crying out for some sort of templating, be it Smarty or something you concoct yourself.

Warboss Alex

6:37 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



For serious projects, use seperate PHP and HTML files. Have them match by name if you want, like:
index.php, index.htm

Define all the variables in your index.php, then include the index.htm file. And this is MUCH easier to maintain, believe you me.

//index.php
$title = 'a title';
$var = 'a var';
include 'index.htm';

Something like that, for a really basic example. After that, make a templating system or use Smarty. I made my own, although Smarty rocks. My life became a WHOLE lot easier after that.

That way, you can even get a web designer to do the HTML for you, and you can concentrate to the -REAL- stuff, heh.

Seriously, though, seperate php and html files is the way to go. It needs a bit of work setting up, but it's a breeze after that.

jatar_k

7:48 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>Seriously, though, seperate php and html files is the way to go.

As with most things we deal with there are no absolutes, it will completely depend on each individual situation.

There is also no way to split them 100% as, at very least, you will be echo'ing vars which is what the original question was anyway. ;)

Hester

9:43 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My pages already use a form of templates. The header and menu are includes, setting variables. The main page calls these, then uses PHP at the base to give me the Last Modified date. So to me there's no way to avoid a mix of PHP and HTML somewhere. I thought this was normal?