Forum Moderators: coopster
...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]
I am in the camp of escpaing out of PHP, simply because I think it reads easier!
HTH
mbcx9rvt
$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.
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
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.)
$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;
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.
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. ;)