How do I execute PHP code in a HTML page?

Avatar
  • updated
  • Answered
Ultimately, I'd like to insert a short line of PHP to get the current date and format it a specific way.

Right now, if I put PHP into my HTML it sends the PHP to the browser!

I need the server to run the PHP and insert the output into my HTML. What do I need to do?

For example, I added two lines to the 'default.htm' that came out of the box. First line is "TESTING..."
Second line is


TESTING...





When I refresh the page, "TESTING..." shows up, but not "HELLO". If I use the browser to inspect the page source, I see my PHP code.

Thanks!
Avatar
anonymous
Quote from UDAY

Thanks for some information, but exactly where the one line  code should be added can you please explain me...?

You can add it among other similar lines or at the top.

Avatar
UDAY
Quote from Tim S.

Hi sandiego,

Thanks for posting your question. I'm more than happy to assist you today. You have two options when facing the problem that your PHP code is not executing in an HTML page. Since the web server thinks the page is written in HTML, it does not know to execute the PHP code. You can save the page as a .php file and this should resolve the issue.

Alternatively, if you have a alot of links to that page, changing the file extension will break all the links. You can tell the server in your .htaccess file to execute all HTML pages as though they are PHP. Here's the code to do that:

AddType application/x-httpd-php .html

This tells the server to execute all files with the .html extension as a PHP file.

If you are using the .htm extension on your files you'd add this code:

AddType application/x-httpd-php .htm

Lastly, if you wanted to execute just one file that's got the extension of .html or .htm as a PHP file you could add this line of code:

<Files yourpage.html> AddType application/x-httpd-php .html </Files>

Remember, you'll need to replace the yourpage.html with your actual file name. If you need further assistance please feel free to contact us. Thanks! Tim S

Thanks for some information, but exactly where the one line  code should be added can you please explain me...?

Avatar
Tim S.

Hi sandiego,

Thanks for posting your question. I'm more than happy to assist you today. You have two options when facing the problem that your PHP code is not executing in an HTML page. Since the web server thinks the page is written in HTML, it does not know to execute the PHP code. You can save the page as a .php file and this should resolve the issue.

Alternatively, if you have a alot of links to that page, changing the file extension will break all the links. You can tell the server in your .htaccess file to execute all HTML pages as though they are PHP. Here's the code to do that:

AddType application/x-httpd-php .html

This tells the server to execute all files with the .html extension as a PHP file.

If you are using the .htm extension on your files you'd add this code:

AddType application/x-httpd-php .htm

Lastly, if you wanted to execute just one file that's got the extension of .html or .htm as a PHP file you could add this line of code:

<Files yourpage.html> AddType application/x-httpd-php .html </Files>

Remember, you'll need to replace the yourpage.html with your actual file name. If you need further assistance please feel free to contact us. Thanks! Tim S