Page 1 of 1
Which is the proper way to create html content in php?
Posted: Sun Jan 24, 2010 8:42 am
by dx2
A bit newbie question but is it right to use echo and create larger blocks of html ot there is another preferred approach?
The best thing I saw up to now is using the echo <<< .How do you prefer to create the dynamic content in your page? It just seems a bit strange useing echo and then write 10 lines string containing html.
Sorry if this has already been discussed.
Re: Which is the proper way to create html content in php?
Posted: Sun Jan 24, 2010 4:21 pm
by JakeJ
Use one of these methods:
Code: Select all
<?php
#write some code
echo '<h1>THIS IS HTML CODE</h1>;
?>
or you can do this:
Code: Select all
<?
#write some php code
?>
<html>
//write some html code
</html>
<?php #return to php
?>
or this:
Code: Select all
<html>
//write a bunch of html code
<?php #write some php
?> //escapes back out of php to html
Which method you use depends on what you're trying to accomplish. Echoing large amounts of html code is no problem so go ahead and do it if suits your needs, it's perfectly acceptable. This is especially true if you have large amounts of php and only a bit of html code. It's much easier than adding in lots of php statements withing the html as the third method shows.
Re: Which is the proper way to create html content in php?
Posted: Sun Jan 24, 2010 5:48 pm
by josh
The absolute best way is to separate your html & php at the file level. The only PHP code that should be in the html would be something like echoing a simple variable. The template file should contain mostly html, and little php. Your scripts should contain mostly PHP and little markup if any.
1 language per file is a reasonable definition of "proper". Although in practice that is cumbersome to follow (I would say its ok to have SQL in your PHP code for example, but it should have it's own file.. example Finder.php that is comprised of mostly simple SQL statements). Try to separate things at the file level (html, sql, etc..).