PHP code in HTML

Moderator: cah

Post Reply
cah
General of the Army / Fleet Admiral / General of the Air Force
General of the Army / Fleet Admiral / General of the Air Force
Posts: 1342
Joined: Sun Aug 17, 2008 5:05 am

PHP code in HTML

Post by cah »

I was trying to test a simple HTML with PHP code in it but it just didn't work.

I had a test.html:

Code: Select all

<html>
<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i; ?></li>
<?php } ?>
</ul>
</body>
</html>
It showed me:

Code: Select all

. Menu Item
with the following html source code:

Code: Select all

<html>
<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i; ?></li>
<?php } ?>
</ul>
</body>
</html>
After searching for some time, I found a post saying I need to change the file's extension from html to php.
I changed the extension and it worked!

Code: Select all

. Menu Item 1
. Menu Item 2
. Menu Item 3
. Menu Item 4
. Menu Item 5
with the html source code:

Code: Select all

<html>
<head></head>
<body>
<ul>
<li>Menu Item 1</li>
<li>Menu Item 2</li>
<li>Menu Item 3</li>
<li>Menu Item 4</li>
<li>Menu Item 5</li>
</ul>
</body>
</html>
The other suggestion to be made in httpd.conf is add .html to the following config:

Code: Select all

AddType application/x-httpd-php .php .html
I tested it and it worked too.

I am just a little concerned about all html pages being rendere/processed that could cause some delay.

It is good to know both work.
CAH, The Great
Post Reply