Tuesday, July 28, 2009

What is best way to write HTML code within a PHP script?

I'm not sure whether it is best practice to use echo, or to escape out of php each time.

What is best way to write HTML code within a PHP script?
I think this is your stylistic choice, that may change from script to script depending on the amount of "boilerplate" text that is to be output.
Reply:If you have a lot of html, then escape php.





or, just include html files like so.





Your url is index.php?page=Contact





Your php would be:





%26lt;?php





include "header.php";





if ($_GET['page']) {


include ".$_GET['page'].".html";


}





include "footer.php";


?%26gt;





Or make it much easier and cleaner with PHP Templates like Smarty.


http://smarty.php.net/
Reply:The best way is to use a variable to hold your page info. Try something like $displaystring = "%26lt;h1 align='center'%26gt;My Heading%26lt;/hi%26gt;


%26lt;form (etc etc). If you need to stop and inset some data you can terminate the string as in ... %26lt;/row%26gt;";


Then run you php data gathering section and continue with


$displaystring .=" ...... and insert the data variables. If you put your php at the top of the page, and the %26lt;html%26gt; after that, you can use %26lt;?php echo $displaystring; ?%26gt; entered as the body of the html.


No comments:

Post a Comment