<?php...?> in html code ignored

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scott1944
Forum Newbie
Posts: 2
Joined: Fri Jul 10, 2009 11:39 am

<?php...?> in html code ignored

Post by scott1944 »

Embedding html in my php files works fine. Embedded php in an html file is ignored. For example, the following produces a blank page but with the head processed. Indeed, all html code is processed and no php code is. Any help offered will be much appreciated.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title> Messing about </title>
</head>
<body>
<p>
 
<?php
echo 'hello world';
?>
 
</p>
</body>
</html>
many thanks
scott
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: <?php...?> in html code ignored

Post by andyhoneycutt »

Is your file a .html file or a .php/.php5 file?

If the former, you'll want to add a directive to apache telling it to process html for php. Otherwise, change it to a .php/.php5 file.

-Andy
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: <?php...?> in html code ignored

Post by genconv »

This is because PHP parser don't parse HTML files (this is default). If you want to parse .html files you must to add this line:

Code: Select all

 
AddHandler application/x-httpd-php .html
 
to the php configuration file.
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: <?php...?> in html code ignored

Post by genconv »

You can also add this line to the .htaccess file:

For Web servers using PHP as apache module:

Code: Select all

AddType application/x-httpd-php .html
For Web servers running PHP as CGI:

Code: Select all

AddHandler application/x-httpd-php .html
scott1944
Forum Newbie
Posts: 2
Joined: Fri Jul 10, 2009 11:39 am

Re: <?php...?> in html code ignored

Post by scott1944 »

Many thanks for responses. I will do as you suggest.
scott
Post Reply