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 17 1423
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-
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.
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!
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
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.'
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>
....
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
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
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
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>
"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.
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-
"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
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
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-
"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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Raymond Hettinger |
last post by:
Comments are invited on the following proposed PEP.
Raymond Hettinger
-------------------------------------------------------
PEP: 329
|
by: F. Petitjean |
last post by:
I have written a script to find the modules which export the largest
number of names. The gc.getreferrers(*objs) function gives also an idea
of the dependencies between the modules.
The code...
|
by: Derek M Jones |
last post by:
All,
My book "The New C Standard: An economic and cultural commentary"
includes a complete analysis of differences between C and C++
from the C point of view (every sentence in the C Standard,...
|
by: benben |
last post by:
Is there an effort to unify the c++ coding standard? Especially
identifier naming.
Not a big issue but it would be annoying to have to incorporate
different coding styles simultaneously when...
|
by: Konrad Palczynski |
last post by:
I am looking for tool to validate conformity to defined coding standard.
I have already found Parasoft's C++ Test, but it is quite expensive. Is
there any Open Source alternative? I do not need...
|
by: Robert Seacord |
last post by:
The CERT/CC has just deployed a new web site dedicated to developing
secure coding standards for the C programming language, C++, and
eventually other programming language.
We have already...
|
by: steven.sagerian |
last post by:
Can anyone suggest a good off the shelf coding standard for embedded
real time applications?
Thanks,
Steve
|
by: =?ISO-8859-1?Q?Arnaud_Carr=E9?= |
last post by:
Hi all,
I guess you all know how difficult it is to choose a conding standard.
And even more difficult it is to explain the choice to your dev team :-)
I'm looking for an "official" c++ coding...
|
by: John Nagle |
last post by:
I hadn't read this before, but I just came back from the
Embedded Systems Conference, where three vendors were selling
checking tools to find bugs in real-time C code. Sometimes
they can detect...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |