468,249 Members | 1,511 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Noob - php works as .php but not when part of html

I'm missing something and need a push in the right direction.
If I create a php file with the following content, and call it via a
browser, it works as expected.
<?php
echo "hello world";
?>

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>

I'm running
Mandrake 10.0
Apache 2
PHP 4
The php_mod is loaded for Apache.
I did the install for both Apache and PHP via Mandrakes built in rpm
management system.

TIA for any tips.

Doug
Jul 17 '05 #1
21 1939
I noticed that Message-ID:
<zq********************@metrocastcablevision.com > from dogu contained
the following:
I'm missing something and need a push in the right direction.
If I create a php file with the following content, and call it via a
browser, it works as expected.
<?php
echo "hello world";
?>

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>


Unless you have configured Apache otherwise, the file must end in .php

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
I noticed that Message-ID: <r8********************************@4ax.com>
from Geoff Berrow contained the following:
If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>


Unless you have configured Apache otherwise, the file must end in .php


Oops. Just noticed an erroneous '>'

try

<html>
<body>
<?php
echo "Hello world";
?>
</body>
</html>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
Geoff Berrow wrote:
I noticed that Message-ID: <r8********************************@4ax.com>
from Geoff Berrow contained the following:

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>


Unless you have configured Apache otherwise, the file must end in .php

Oops. Just noticed an erroneous '>'

try

<html>
<body>
<?php
echo "Hello world";
?>
</body>
</html>

Thanks, but what I posted was a type - in my actual code, the <?php line
is identical to yours.

What do you mean 'unless you have configured Apache otherwise...'? I
bet that's the problem but don't know what to configure.

Any tips would be appreciated.

Doug
Jul 17 '05 #4
Even with HTML intermingled amongst the PHP code, it's still a .php
file.
rename it whatever.php.

Of course you could tell apache to parse .html files as .php, but that
would be very bad advice.

dogu <df***********@netscape.net> wrote in message news:<zq********************@metrocastcablevision. com>...
I'm missing something and need a push in the right direction.
If I create a php file with the following content, and call it via a
browser, it works as expected.
<?php
echo "hello world";
?>

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>

I'm running
Mandrake 10.0
Apache 2
PHP 4
The php_mod is loaded for Apache.
I did the install for both Apache and PHP via Mandrakes built in rpm
management system.

TIA for any tips.

Doug

Jul 17 '05 #5
Geoff Berrow wrote:
I noticed that Message-ID: <r8********************************@4ax.com>
from Geoff Berrow contained the following:

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>


Unless you have configured Apache otherwise, the file must end in .php

Oops. Just noticed an erroneous '>'

try

<html>
<body>
<?php
echo "Hello world";
?>
</body>
</html>

Seems my confusion was thinking you could co-mingle php and html code in
files with html extensions. If I change the extension to .php it works
like a charm!

Take care.

Doug
Jul 17 '05 #6
dogu wrote:
Geoff Berrow wrote:
I noticed that Message-ID: <r8********************************@4ax.com>
from Geoff Berrow contained the following:

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>
Unless you have configured Apache otherwise, the file must end in .php


Oops. Just noticed an erroneous '>'

try

<html>
<body>
<?php
echo "Hello world";
?>
</body>
</html>

Seems my confusion was thinking you could co-mingle php and html code in
files with html extensions. If I change the extension to .php it works
like a charm!

Take care.

Doug


Bet you can set up the server to treat html files as php :)
Jul 17 '05 #7
*** dogu escribió/wrote (Sun, 17 Oct 2004 10:32:42 -0400):
Seems my confusion was thinking you could co-mingle php and html code in
files with html extensions. If I change the extension to .php it works
like a charm!


There's no point in feeding *all* pages to the PHP interpreter. The *.php
extension is the way to parse only pages that contain code.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Jul 17 '05 #8
Did you check your web server configuration?

In Apache on Linux, you might add:

AddType application/x-httpd-php .php .html

This will allow you to include PHP in your HTML as

<HTML>

blah blah blah...

<?php

php stuff blah blah blah

?>

</HTML>
You may have to have

AddOutputFilter INCLUDES .shtml
AddOutputFilter INCLUDES .html

added as well, but don't have the time to test. These turn on SSI's on
your pages, but I don't know if they affect embedded PHP or not. At
any rate I have both on my server and they work.

-DG-

dogu wrote:
Geoff Berrow wrote:
I noticed that Message-ID: <r8********************************@4ax.com>
from Geoff Berrow contained the following:

If I embed the same thing in a simple HTML file, I get nothing.
<html>
<body>
<?php>
echo "Hello world";
?>
</body>
</html>
Unless you have configured Apache otherwise, the file must end in .php


Oops. Just noticed an erroneous '>'

try

<html>
<body>
<?php
echo "Hello world";
?>
</body>
</html>

Seems my confusion was thinking you could co-mingle php and html code in
files with html extensions. If I change the extension to .php it works
like a charm!

Take care.

Doug


Jul 17 '05 #9
Data Goob wrote:
Did you check your web server configuration?

In Apache on Linux, you might add:

AddType application/x-httpd-php .php .html


You probably want to put this in a .htaccess, so that it is NOT server-wide.
Jul 17 '05 #10
Right Roo, good point.

You could also do the Virtual host thang as well but I haven't tested, so I'm
unsure as to the rightness, truth, etc.

-DG-

Michiel de Roo wrote:
Data Goob wrote:
Did you check your web server configuration?

In Apache on Linux, you might add:

AddType application/x-httpd-php .php .html

You probably want to put this in a .htaccess, so that it is NOT
server-wide.


Jul 17 '05 #11
I noticed that Message-ID:
<ha********************@metrocastcablevision.com > from dogu contained
the following:

What do you mean 'unless you have configured Apache otherwise...'? I
bet that's the problem but don't know what to configure.


What Brad said...

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #12
Well, now you've got my curiousity...

Why is it a bad idea to intermingle PHP with HTML?

Wasn't that the intent of PHP in the first place?

FWIW I'm not arguing one way or the other, just interested
in your opinion(s) .

-DG-

Geoff Berrow wrote:
I noticed that Message-ID:
<ha********************@metrocastcablevision.com > from dogu contained
the following:

What do you mean 'unless you have configured Apache otherwise...'? I
bet that's the problem but don't know what to configure.

What Brad said...


Jul 17 '05 #13
In article <nW***************@fe05.usenetserver.com>,
Data Goob <da******@hotmail.com> wrote:
Well, now you've got my curiousity...

Why is it a bad idea to intermingle PHP with HTML?

Wasn't that the intent of PHP in the first place?

FWIW I'm not arguing one way or the other, just interested
in your opinion(s) .

-DG-

Geoff Berrow wrote:
I noticed that Message-ID:
<ha********************@metrocastcablevision.com > from dogu contained
the following:

What do you mean 'unless you have configured Apache otherwise...'? I
bet that's the problem but don't know what to configure.

What Brad said...


I've read many opinions on this. It all seems to revolve around the
scope of the project you're working on. Medium to large projects tend
to bog down with the code that generates the site and the actual HTML
itself. My own project is now to the point that I need to refactor it
to strip out embedded HTML and use a templating system instead, just to
make it easier to understand and support and grow.

I'm now seeing the result of forgetting stuff I coded months ago and
having to refamiliarize myself with it when I go back and update it.
I'm leaning toward a OOP page display class with templates.

In any case, as an old procedure coder who first learned FORTRAN in the
early 1970's and never used C very much (automatic variables really
confused me initially), it's been interesting having to rethink this
project.

Now I see why the database weenies like using the database as a
repository for the datastructures and business logic (i.e. Oracle's
triggers or MySQL's stored procedures in the next release) and the
application does the data capture, validation, and presentation.

--
DeeDee, don't press that button! DeeDee! NO! Dee...

Jul 17 '05 #14
I noticed that Message-ID: <nW***************@fe05.usenetserver.com>
from Data Goob contained the following:
Well, now you've got my curiousity...

Why is it a bad idea to intermingle PHP with HTML?


I don't believe it is, necessarily.

But by default the resultant file has to be named <something>.php
whether it contains just raw php or a mixture of php and html.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #15
Geoff Berrow wrote:
I noticed that Message-ID: <nW***************@fe05.usenetserver.com>
from Data Goob contained the following:

Well, now you've got my curiousity...

Why is it a bad idea to intermingle PHP with HTML?

I don't believe it is, necessarily.

But by default the resultant file has to be named <something>.php
whether it contains just raw php or a mixture of php and html.

Actually this is not true. I have several HTML files with PHP
embedded. In fact they are not only with embedded PHP the PHP
statement is a PHP include, and I have SSI's as well... with the
..html suffix.
<HTML>
blah blah blah...

<?php include 'my-embedded-calendar.php' ?>

more HTML...

</HTML>

So what it really boils down to is in getting Apache to see .html
as something that can be interpreted as/with PHP. As a purist,
this might seem a violation and comingling of two different things,
but for me it is hybridization and the ability to component-ize
my sites for easier maintenance. Is it right for all web work?
Probably not appropriate in all situations, and may even create
problems with other add-ons like templates, etc. I view templates
as hybridization anyway, so for me the discussion is purely
academic. Template integration reduces portability, so if this is
the intent, don't do non-portable PHP/SSI/HTML/TEMPLATES/ETC.

In the final analysis, it works beautifully for my sites...
<HTML>

blah blah blah...

<!--include virtual="my-included-banner.html-->

<?php include 'my-embedded-calendar.php' ?>

more HTML...

</HTML>

Regards,

-DG-

Jul 17 '05 #16
Data Goob wrote:

Well, now you've got my curiousity...

Why is it a bad idea to intermingle PHP with HTML?


It isn't.

But when you do so you should name the file *.php, so that the server
knows it has to parse the file through php.

Alternativly you could tell the server to parse every file through php
but *that* is not a good idea. If all of your pages contain php it
wouldn't make any difference but if this is not the case the server goes
through quite a bit of unnecessary overhead.

That might not matter on your personal server that is only used for
testing, but no provider is going to configure his server like that
because less overhead means they can put more customers on one server.
So if you want your pages to run on any provider's server without having
to change them you should go with the usual file naming.

In other words: If it contains php name it *.php.

Bye!
Jul 17 '05 #17
.oO(Alvaro G. Vicario)
*** dogu escribió/wrote (Sun, 17 Oct 2004 10:32:42 -0400):
Seems my confusion was thinking you could co-mingle php and html code in
files with html extensions. If I change the extension to .php it works
like a charm!


There's no point in feeding *all* pages to the PHP interpreter. The *.php
extension is the way to parse only pages that contain code.


I consider it bad style to have both .html and .php appear in URLs.

On Apaches it's possible to parse some files with PHP and deliver others
directly, while keeping _all_ URLs consistent with the extension .html
(MultiViews).

Micha
Jul 17 '05 #18
Michael Fesser wrote:
I consider it bad style to have both .html and .php appear in URLs.
Is there a reason for either?
On Apaches it's possible to parse some files with PHP and deliver others
directly, while keeping _all_ URLs consistent with the extension .html
'.html' and other URI suffixes are redundant, contrary to
the principle of keeping URIs short and rememberable, and
deleterious to URI persistency. As a rule then it's best to
chop the wee nasties off.
(MultiViews).


Right.

Slainte! & HAGW!

--
Jock
Jul 17 '05 #19
.oO(John Dunlop)
Michael Fesser wrote:
I consider it bad style to have both .html and .php appear in URLs.
Is there a reason for either?


Not really. I use either .html or no extension at all.
'.html' and other URI suffixes are redundant, contrary to
the principle of keeping URIs short and rememberable, and
deleterious to URI persistency.
There's at least one point: Unexperienced users might expect web
addresses to start with 'www.' and end on '.html'. Sure we know that
both are not really necessary ...
As a rule then it's best to
chop the wee nasties off.


That's what I will do in my next (testing) project, but for other
reasons. In short: With a URL like www.example.com/foo the server will
automatically decide according to the Accept header whether to send HTML
or XHTML back to the client. But I still want to be able to override the
content negotiation and force a particular content type if necessary, be
it for testing, for validation or simply for providing the same resource
in different formats (PDF for example). So to override the CN mechanismn
I would simply request /foo.html, /foo.xhtml or /foo.pdf if available.

That's just the very basic idea, I still have to think about some things
and implementation issues.

Micha
Jul 17 '05 #20
Michael Fesser wrote:
There's at least one point: Unexperienced users might expect web
addresses to start with 'www.' and end on '.html'. Sure we know that
both are not really necessary ...
Fair enough. Although with the myriad different suffixes
out there today, '.php', '.asp', '.pl', '.cf' to name but a
few, I'm still unconvinced that surfers, inexperienced or
not, expect URIs to end in '.html'.

Also, I am seeing more and more URIs without suffixes on
television. Rightpondians can see the BBC's abbreviated
URIs, e.g. www.bbc.co.uk/weather -- that one's unfortunately
a redirection; hey, the beeb can't get it right all the
time, can they? If the big guns publish sensible URIs, the
practice will hopefully catch on soon.

[about chopping off URI suffixes]
That's what I will do in my next (testing) project, but for other
reasons. In short: With a URL like www.example.com/foo the server will
automatically decide according to the Accept header whether to send HTML
or XHTML back to the client. But I still want to be able to override the
content negotiation and force a particular content type if necessary, be
it for testing, for validation or simply for providing the same resource
in different formats (PDF for example). So to override the CN mechanismn
I would simply request /foo.html, /foo.xhtml or /foo.pdf if available.
Sounds good to me.
That's just the very basic idea, I still have to think about some things
and implementation issues.


This might be of value:

http://groups.google.com/groups?th=36072a520721e723

Slainte!

--
Jock
Jul 17 '05 #21
.oO(John Dunlop)
This might be of value:

http://groups.google.com/groups?th=36072a520721e723


Yep. Thanks.

Micha
Jul 17 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dave Williams | last post: by
8 posts views Thread by Rich Grise | last post: by
4 posts views Thread by shumaker | last post: by
reply views Thread by AndyW | last post: by
2 posts views Thread by kj | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.