Re: Help! What about this method to separate HTML code from PHP code?
| From: | Mark Gallagher | Date: | Sat, 24 Aug 2002 10:03:40 +0000 |
| Subject: | Re: Help! What about this method to separate HTML code from PHP code? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-113922@lists.php.net to get a copy of this message | ||
jianking wrote:
> Help! What about this method to separate HTML code from PHP code?
>
> I don't like the HTML code in the PHP code. So I put them in a file.
>
> For example:
>
> <====the old file====>
>
> <---index.php--->
>
> <?
> echo "<html>";
> echo "<body>";
> echo "This is a example file!";
> echo "</body>";
> echo "</html>";
> ?>
One of the coolest things about PHP is you don't have to do this to use
HTML. There's nothing wrong with:
<html>
<body>
This is a example file!
</body>
</html>
Well, there is, really. You're missing a DOCTYPE[0], and all documents
are required to contain a <head>, <title>, and <body>. It's also bad
practice (though technically allowable in everything except XHTMLx.x
Strict) to include text without any containing block-level elements
(<body> doesn't count). Also, think grammar. So:
<html>
<head>
<title>
Foo
</title>
</head>
<body>
<p>
This is an example file!
</p>
</body>
</html>
Any PHP you want to include there is then the recipient of your magic
<?php ?> delimeters (or <? ?> if available).
> <====method====>
>
> I put the HTML code into a file, and use it by "include" or "require".
>
> <---index.html.php--->
>
> <html>
> <body>
> This is a example file!
> </body>
> </html>
>
> <---index.php--->
>
> <?
> include "index.html.php";
> ?>
>
> Can you tell me this method is good?
> If not, please tell me the best method!
It's kind of an inside-out way of looking at things. The HTML contains
the PHP, *not* the other way 'round.
If you want to keep the *HTML* "clean", and have all code (including
server-side stuff) in a separate file, then have a normal HTML file, and
include the script:
<?
include "index.php";
?>
instead. This way you've got your HTML and your PHP separate, but you
aren't twisting your website's structure inside-out.
[0] http://allmyfaqs.com/faq.pl?DOCTYPE
--
Mark Gallagher
Desperately attempting - and failing - to stay on topic since 1999
fuddleriffic - http://cyberfuddle.com/
blog - http://cyberfuddle.com/infinitebabble/