How to get the php code working inside html file?

21,373

Solution 1

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.

Solution 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

Solution 3

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
Share:
21,373
Alex
Author by

Alex

Updated on September 18, 2022

Comments

  • Alex
    Alex almost 2 years

    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?

  • Alex
    Alex about 11 years
    Yes thats it! Thaks a lot.
  • David Houde
    David Houde about 11 years
    No prob and good luck!
  • Alex
    Alex about 11 years
    Yes found this as well, but does not work. Maybe permissions on the file were wrong.
  • n1kita
    n1kita about 11 years
    Are you sure about your php version? If it's not version 5 then try this: AddType application/x-httpd-php .html