0

Following an example of using php code inside html on the W3 page I have the following code inside my html code:

<!DOCTYPE html>
<html>
<body>

<p>First try with php:</p>


<?php
echo "My first PHP script!";
?>

</body>
</html> 

But only the text in the paragraph tags is shown (when opening the html page in a browser). Is there something wrong with the syntax? Do I need to configure something else in some setup?

Additional information: When I browse to a file test.php with the following content

<?php phpinfo() ?>

I get a whole bunch of configuration information; this shows me that apache is correctly configured for php. But why does the simple example above not work then?

3 Answers 3

4

You did not clarify, so my best guess would be that your file has a .html extension.

You may combine PHP and HTML in a single document, but you typically need to give it a .php extension in order for the code to be parsed. There is also the .phtml extension used by some, but I typically try to avoid using it for anything but Views (Google MVC if you really want to know).

Rename your index.html to index.php and that should take care of it. Unless this is done already, then please update us.

2
  • Yes thats it! Thaks a lot.
    – Alex
    Apr 6, 2013 at 9:27
  • No prob and good luck! Apr 6, 2013 at 9:31
2

Just Create a .htaccess file in your root directory and then copy following line and then save.

AddHandler application/x-httpd-php5 .html .php .htm

Now code within any .html or .htm files will work as php so you dont need to change extension from .html to php

1

If you're using Apache as webserver you can write a .htaccess file to parse html files as php files. Here is an example

AddHandler application/x-httpd-php5 .html
2
  • Yes found this as well, but does not work. Maybe permissions on the file were wrong.
    – Alex
    Apr 6, 2013 at 12:27
  • Are you sure about your php version? If it's not version 5 then try this: AddType application/x-httpd-php .html
    – n1kita
    Apr 6, 2013 at 12:31

Not the answer you're looking for? Browse other questions tagged .