470,826 Members | 2,363 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

standard coding...

Hi all,

I was wondering if there is a standard when mixing PHP and HTML script on a
page... and if there were any articles on this subject...

i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..

Obviously this only applies to pages where a mixture of php and html is
required...

for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
</body>
</html>

saved as index.html

or
<?php
echo '<html>';
echo '<head>';
echo '<title>title text</title>';
echo '</head>';
echo '<body>';
echo 'Your name is '.$name;
echo '</body>';
echo '</html>';
?>

saved as index.php

TIA
Andy Mak
Jan 12 '06 #1
17 1337
Domestos wrote:
Hi all,

I was wondering if there is a standard when mixing PHP and HTML script on a
page... and if there were any articles on this subject...

i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..

Obviously this only applies to pages where a mixture of php and html is
required...

for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
</body>
</html>

saved as index.html

or
<?php
echo '<html>';
echo '<head>';
echo '<title>title text</title>';
echo '</head>';
echo '<body>';
echo 'Your name is '.$name;
echo '</body>';
echo '</html>';
?>

saved as index.php

TIA
Andy Mak

I prefer the former style. I find it much easier when dealing with
screens and screens of HTML. Of course, the first example should still
probably be saved as index.php...

-david-

Jan 12 '06 #2
Domestos,

If you're early in the development of a site, take a look at Smarty
(smarty.php.net). It's very beneficial (to me and others) as it lets
you seperate out your HTML markup and PHP code.

It has a nice syntax, with the ability to do things like looping in the
HTML, as well as caching.

Jan 12 '06 #3
Domestos wrote:
I was wondering if there is a standard when mixing PHP and HTML script on a
page... and if there were any articles on this subject...

i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..
The "standard" installation of PHP on Apache *does not* make Apache call
PHP for files with an "html" extension. To make the PHP embedded in
..html work you have to reconfigure the webserver. If you switch server
all your .html pages will stop working until you reconfigure the new
server.

So it's better to make certain pages where PHP is needed have a .php
extension :)

On the other hand, if you have a .php file with no php inside, the
server will call PHP without needing to, making the page (and the
server) slower and wasting resources.
Obviously this only applies to pages where a mixture of php and html is
required...

for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
You name is <?php echo $name; ?>
</body>
</html>

saved as index.html
There's php inside, so save as index.php
<?php
echo '<html>';
echo '<head>';
echo '<title>title text</title>';
echo '</head>';
echo '<body>';
echo 'Your name is '.$name;
echo '</body>';
echo '</html>';
?>

saved as index.php


Ugh!

You can also do

<?php
echo <<<HTML
<html>
<head>
<title>title text</title>
</head>
<body>
You name is $name
</body>
</html>
HTML;
?>

and save as foobar.php

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jan 12 '06 #4
On Thu, 12 Jan 2006 00:10:02 GMT, "Domestos" <ne*******@mind.com> wrote:
i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..

for example... which is better or more standard....
It Depends.

Basically on the ratio of PHP versus HTML. For large blocks of HTML with only
small numbers of PHP variables to print, dropping out of PHP mode and into
literal HTML with small <?php echo $x ?> blocks makes sense.

For places where it's predominantly PHP code with a few lines of HTML printed,
e.g. in loops, echo/print makes sense.

Neither's more correct or more standard - it's all down to readability in
context, and personal preference, or coding standards in your organisation if
you're working in a team.
<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>


But this example won't work...

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jan 12 '06 #5
On 2006-01-12, Domestos <ne*******@mind.com> wrote:
for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
</body>
</html>

saved as index.html

or
<?php
echo '<html>';
echo '<head>';
echo '<title>title text</title>';
echo '</head>';
echo '<body>';
echo 'Your name is '.$name;
echo '</body>';
echo '</html>';
?>

saved as index.php

Personally, in that example I'd use the first style of coding, and save it as
index.php

I have my servers set to only parse .php files for PHP code, so if I saved it
as a .html file it wouldn't work. It saves looking for PHP when there is none,
probably doesn't make much of a difference though.

--
J
My Realm: http://www.myrealm.co.uk/
LORE: http://lore.myrealm.co.uk/
'Drink up. The worlds about to end.'
Jan 12 '06 #6
Domestos wrote:
Hi all,

I was wondering if there is a standard when mixing PHP and HTML script on a
page... and if there were any articles on this subject...
Not really. That's one nice thing about PHP--there is no orthodoxy.
for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
</body>
</html>


That's the style I prefer. Echo HTML from PHP becoming a huge pain when
the page makes use of Javascript. Instead of plain-o echo, I usually
use a function with a short name, as almost nearly everything output
needs to be escaped for HTML special characters.

Example:

function P($s, $flags = 0) {
$s = htmlspecialchars($s);
if(!($flags & NO_BR_TAGS)) {
$s = nl2br($s);
}
echo $s;
}

Then scatter throughout my HTML:

.....
<tr><td><a href="<? P($url) ?>"><? P($title) ?></a></td></tr>
....

Jan 12 '06 #7
On Thu, 12 Jan 2006 00:10:02 +0000, Domestos wrote:
Hi all,

I was wondering if there is a standard when mixing PHP and HTML script on a
page... and if there were any articles on this subject...

i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..
PHP is executed by a web server. Web servers normally serve HTML pages
and know how to deal with PHP. PHP is embedded language - so embed it.
Why would you want to use embedded language to generate HTML page that
will in turn embed PHP?

Obviously this only applies to pages where a mixture of php and html is
required...

for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
</body>
</html>

saved as index.html

or
<?php
echo '<html>';
echo '<head>';
echo '<title>title text</title>';
echo '</head>';
echo '<body>';
echo 'Your name is '.$name;
echo '</body>';
echo '</html>';
?>

saved as index.php

TIA
Andy Mak


--
http://www.mgogala.com

Jan 12 '06 #8
On Thu, 12 Jan 2006 00:10:02 +0000, Domestos wrote:
Hi all,

I was wondering if there is a standard when mixing PHP and HTML script on a
page... and if there were any articles on this subject...

i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..

Obviously this only applies to pages where a mixture of php and html is
required...

for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
</body>
</html>

saved as index.html

or
<?php
echo '<html>';
echo '<head>';
echo '<title>title text</title>';
echo '</head>';
echo '<body>';
echo 'Your name is '.$name;
echo '</body>';
echo '</html>';
?>

saved as index.php

TIA
Andy Mak


Personally, I'd create a function that returns a string that displays the
head, body, etc, so I can write

<?
echo html ( head( title ( "title text" ) ) .
body ( "Your name is $name" ) );
?>

I detest the messy embedding of php in html, it makes stuff almost
impossible to debug.

But then that's my point of view, and I am/was a C programmer and fan of
Dijkstra at heart. I always read what Mladen posts, but he disagrees
completely with me on this.

So there you have it (:
Steve
Jan 12 '06 #9
On Wed, 11 Jan 2006 17:11:51 -0800, bobzimuta wrote:
Domestos,

If you're early in the development of a site, take a look at Smarty
(smarty.php.net). It's very beneficial (to me and others) as it lets
you seperate out your HTML markup and PHP code.

It has a nice syntax, with the ability to do things like looping in the
HTML, as well as caching.


I prefer to use etomite ( http://etomite.org ). I find their use of
snippets of php to be really easy both to comprehend and use.

Steve
Jan 12 '06 #10
Following on from Domestos's message. . .
Hi all,

I was wondering if there is a standard when mixing PHP and HTML script on a
page... and if there were any articles on this subject...

i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..

Obviously this only applies to pages where a mixture of php and html is
required...


HTML is always required of course.

My style is to produce 99% of my pages via PHP even if they are static
text. The reason is that the look and feel and other functionality of
my pages is standardised and /manufactured/. So for example I can
programatically change from normal stylesheet to one for visually
impaired, keep a 'back to' page list, and loads of other things in the
page head (and foot - and standard page structure)
--
PETER FOX Not the same since the porcelain business went down the pan
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jan 12 '06 #11
d
"Domestos" <ne*******@mind.com> wrote in message
news:u7***********@newsfe3-win.ntli.net...
Hi all,

I was wondering if there is a standard when mixing PHP and HTML script on
a
page... and if there were any articles on this subject...

i.e. do I make every page a .php page on my website and echo html when I
need to or...

should I make every page html and embed php when I need to..

Obviously this only applies to pages where a mixture of php and html is
required...

for example... which is better or more standard....

<html>
<head>
<title>title text</title>
</head>
<body>
You name is <?php $name?>
</body>
</html>

saved as index.html

or
<?php
echo '<html>';
echo '<head>';
echo '<title>title text</title>';
echo '</head>';
echo '<body>';
echo 'Your name is '.$name;
echo '</body>';
echo '</html>';
?>

saved as index.php

TIA
Andy Mak


Ideally speaking, any file that spits out html should have an .html
extension, as when the client sees it, it's HTML (regardless of how it was
created). As others have pointed out, that will require a minor change in
your server's configuration, but yields more professional results.

Personally, I use a templating toolkit (I wrote my own), which handily
seperates HTML from PHP entirely. Having them both in the same file is
terrible from a production standpoint.
Jan 12 '06 #12
d wrote:
Ideally speaking, any file that spits out html should have an .html
extension, as when the client sees it, it's HTML (regardless of how it was
created). As others have pointed out, that will require a minor change in
your server's configuration, but yields more professional results.


I'm not sure I agree with that. Isn't this a case of confusing naming
with content? I know that, for reasons of speed, web servers like to tag
content based on file suffixes, but I don't recall any of the standards
bodies specifying it.

Also, isn't a file that contains php a '.php'-suffixed file until it is
processed and *then* becomes an HTML image? If so, the file doesn't
'spit out HTML' until it is preprocessed by php.

AFAIK, the main reason for over-riding the .html extension to include
php processing is so that the file displayed in the browser does not
include the '.php' suffix and, therefore, does not tip off the end-user
as to which preprocessing technology is being used. This was thought to
be a security-related practice. It is similar to changing the '.do'
suffix in struts to something else. (Security through obscurity.)

There's nothing magical about any suffix - it's just what you tell the
server to be sensitive to. For example, I could change all the php files
to have a 'pre' suffix to indicate that these files are 'pre'processed
before displaying them.

-david-

Jan 12 '06 #13
d
"David Haynes" <da***********@sympatico.ca> wrote in message
news:OH****************@fe71.usenetserver.com...
d wrote:
Ideally speaking, any file that spits out html should have an .html
extension, as when the client sees it, it's HTML (regardless of how it
was created). As others have pointed out, that will require a minor
change in your server's configuration, but yields more professional
results.
I'm not sure I agree with that. Isn't this a case of confusing naming with
content? I know that, for reasons of speed, web servers like to tag
content based on file suffixes, but I don't recall any of the standards
bodies specifying it.

Also, isn't a file that contains php a '.php'-suffixed file until it is
processed and *then* becomes an HTML image? If so, the file doesn't 'spit
out HTML' until it is preprocessed by php.


So the file, once spat out, should be called .html. Most web servers still
call php files .php even after they've been passed.
AFAIK, the main reason for over-riding the .html extension to include php
processing is so that the file displayed in the browser does not include
the '.php' suffix and, therefore, does not tip off the end-user as to
which preprocessing technology is being used. This was thought to be a
security-related practice. It is similar to changing the '.do' suffix in
struts to something else. (Security through obscurity.)
No, it's just a matter of having extensions matching the contents of the
file. I guess it's just being polite.
There's nothing magical about any suffix - it's just what you tell the
server to be sensitive to. For example, I could change all the php files
to have a 'pre' suffix to indicate that these files are 'pre'processed
before displaying them.
Unfortunately, as long as certain browsers take notice of suffixes, they are
somewhat magical. Making sure your extensions match your content is one
sure-fire way to get round that :)
-david-


another david
Jan 16 '06 #14
d said the following on 16/01/2006 15:19:
"David Haynes" <da***********@sympatico.ca> wrote in message
news:OH****************@fe71.usenetserver.com...
There's nothing magical about any suffix - it's just what you tell the
server to be sensitive to. For example, I could change all the php files
to have a 'pre' suffix to indicate that these files are 'pre'processed
before displaying them.


Unfortunately, as long as certain browsers take notice of suffixes, they are
somewhat magical. Making sure your extensions match your content is one
sure-fire way to get round that :)


That's not always possible, though. Yes, one could set up the server to
parse all .html requests as PHP, but what about dynamic images generated
by PHP? To "match" the suffix there, you'd need to set the server to
parse all .jpg, etc. files as PHP, which would be a sure-fire way to get
bizarre behaviour ;)

--
Oli
Jan 16 '06 #15
d wrote:
"David Haynes" <da***********@sympatico.ca> wrote in message
news:OH****************@fe71.usenetserver.com...
d wrote:
Ideally speaking, any file that spits out html should have an .html
extension, as when the client sees it, it's HTML (regardless of how it
was created). As others have pointed out, that will require a minor
change in your server's configuration, but yields more professional
results. I'm not sure I agree with that. Isn't this a case of confusing naming with
content? I know that, for reasons of speed, web servers like to tag
content based on file suffixes, but I don't recall any of the standards
bodies specifying it.

Also, isn't a file that contains php a '.php'-suffixed file until it is
processed and *then* becomes an HTML image? If so, the file doesn't 'spit
out HTML' until it is preprocessed by php.


So the file, once spat out, should be called .html. Most web servers still
call php files .php even after they've been passed.


At this point the file is simply a byte-stream of HTML code with HTTP
directives prepended. If you're going to be pedantic about it, it would
have a type of 'http' at this point.
AFAIK, the main reason for over-riding the .html extension to include php
processing is so that the file displayed in the browser does not include
the '.php' suffix and, therefore, does not tip off the end-user as to
which preprocessing technology is being used. This was thought to be a
security-related practice. It is similar to changing the '.do' suffix in
struts to something else. (Security through obscurity.)


No, it's just a matter of having extensions matching the contents of the
file. I guess it's just being polite.


Polite to whom? The web server or the maintainer? I suspect you are
referring to the latter. If so, I, as a maintainer, would rather know
the file contained php directives (or asp or jsp etc.) than that it
would eventually render to HTML. The web server is ambivalent as to the
name of the file *except* with respect to type processing (see below).
There's nothing magical about any suffix - it's just what you tell the
server to be sensitive to. For example, I could change all the php files
to have a 'pre' suffix to indicate that these files are 'pre'processed
before displaying them.


Unfortunately, as long as certain browsers take notice of suffixes, they are
somewhat magical. Making sure your extensions match your content is one
sure-fire way to get round that :)


Browsers don't care about suffixes - servers do; but, primarily, only as
they have been taught to care. In apache, it is as easy to add:

AddType application/x-httpd-php foo

as it is to add

AddType application/x-httpd-php php

in which case all my '.foo' files would be processed via php and,
equally important, show as xxx.foo in the browser URL line. Therein lies
the albeit weak security.

What you are proposing is that every file display in the browser's URL
line as 'xxx.html', which is just a specific case of what I have said
above. (replace 'foo' with 'html'). If you do that, however, even plain
HTML files will go through the php interpreter before being emitted by
the server. This may lead to performance penalties on larger systems.

-david-
Jan 16 '06 #16
d
"David Haynes" <da***********@sympatico.ca> wrote in message
news:xv****************@fe56.usenetserver.com...
d wrote:
"David Haynes" <da***********@sympatico.ca> wrote in message
news:OH****************@fe71.usenetserver.com...
d wrote:
Ideally speaking, any file that spits out html should have an .html
extension, as when the client sees it, it's HTML (regardless of how it
was created). As others have pointed out, that will require a minor
change in your server's configuration, but yields more professional
results.
I'm not sure I agree with that. Isn't this a case of confusing naming
with content? I know that, for reasons of speed, web servers like to tag
content based on file suffixes, but I don't recall any of the standards
bodies specifying it.

Also, isn't a file that contains php a '.php'-suffixed file until it is
processed and *then* becomes an HTML image? If so, the file doesn't
'spit out HTML' until it is preprocessed by php.
So the file, once spat out, should be called .html. Most web servers
still call php files .php even after they've been passed.


At this point the file is simply a byte-stream of HTML code with HTTP
directives prepended. If you're going to be pedantic about it, it would
have a type of 'http' at this point.


HTTP is the protocol, not the file. HTTP is being used to move an HTML file
from the server to the client.
AFAIK, the main reason for over-riding the .html extension to include
php processing is so that the file displayed in the browser does not
include the '.php' suffix and, therefore, does not tip off the end-user
as to which preprocessing technology is being used. This was thought to
be a security-related practice. It is similar to changing the '.do'
suffix in struts to something else. (Security through obscurity.)


No, it's just a matter of having extensions matching the contents of the
file. I guess it's just being polite.


Polite to whom? The web server or the maintainer? I suspect you are
referring to the latter. If so, I, as a maintainer, would rather know the
file contained php directives (or asp or jsp etc.) than that it would
eventually render to HTML. The web server is ambivalent as to the name of
the file *except* with respect to type processing (see below).


I'm not saying the two are mutually exclusive. I have a server that
contains many .php files, and they show up as .html files once rendered.
It's not hard.
There's nothing magical about any suffix - it's just what you tell the
server to be sensitive to. For example, I could change all the php files
to have a 'pre' suffix to indicate that these files are 'pre'processed
before displaying them.


Unfortunately, as long as certain browsers take notice of suffixes, they
are somewhat magical. Making sure your extensions match your content is
one sure-fire way to get round that :)


Browsers don't care about suffixes - servers do; but, primarily, only as
they have been taught to care. In apache, it is as easy to add:


Incorrect. Take a look at some of the quirks IE has when looking at
extensions. Granted - they shouldn't look at the extensions, but they sure
as hell do.
AddType application/x-httpd-php foo

as it is to add

AddType application/x-httpd-php php

in which case all my '.foo' files would be processed via php and, equally
important, show as xxx.foo in the browser URL line. Therein lies the
albeit weak security.
Yes, if you leave it like that.
What you are proposing is that every file display in the browser's URL
line as 'xxx.html', which is just a specific case of what I have said
above. (replace 'foo' with 'html'). If you do that, however, even plain
HTML files will go through the php interpreter before being emitted by the
server. This may lead to performance penalties on larger systems.
That's not what I'm saying at all. And on Apache running php as a module,
you'll find there's barely any hit at all when passing .html as .php.
-david-


Not everyone runs a web server that simply parses and spits out files in a
document root. If you're not doing that any more, you can have greater
flexibility in your naming conventions, which makes it more than possible to
have the correct extensions on your files.
Jan 16 '06 #17
d
"Oli Filth" <ca***@olifilth.co.uk> wrote in message
news:mo****************@newsfe3-win.ntli.net...
d said the following on 16/01/2006 15:19:
"David Haynes" <da***********@sympatico.ca> wrote in message
news:OH****************@fe71.usenetserver.com...
There's nothing magical about any suffix - it's just what you tell the
server to be sensitive to. For example, I could change all the php files
to have a 'pre' suffix to indicate that these files are 'pre'processed
before displaying them.
Unfortunately, as long as certain browsers take notice of suffixes, they
are somewhat magical. Making sure your extensions match your content is
one sure-fire way to get round that :)


That's not always possible, though. Yes, one could set up the server to
parse all .html requests as PHP, but what about dynamic images generated
by PHP? To "match" the suffix there, you'd need to set the server to parse
all .jpg, etc. files as PHP, which would be a sure-fire way to get bizarre
behaviour ;)


If you're using a basic webserver setup where every request translates
directly to a file in the directory structure, then yes. That's not what
I'm saying though ;)


--
Oli

Jan 16 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by F. Petitjean | last post: by
reply views Thread by Derek M Jones | last post: by
13 posts views Thread by benben | last post: by
50 posts views Thread by Konrad Palczynski | last post: by
7 posts views Thread by Robert Seacord | last post: by
4 posts views Thread by steven.sagerian | last post: by
8 posts views Thread by =?ISO-8859-1?Q?Arnaud_Carr=E9?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.