When a web page is call, the server checks two thongs, whether it is a html/htm file or a php/asp(etc) file. The key point is file extension or file type.If it is a normal .html/.htm file, server sends it direct to the browser. And if it is a php/asp(etc), server than execute the appropriate code before sending it to the browser.
Come to the point: Now you need to add some php code within your existing html file,then what is the process?
There are two ways, what i ever know and want to share with you,
1.Rename the html file to php file (the easiest way),
2.Using .htaccess file to allow php within html file.
if you follow the first one, it’s easy. but you may have incoming links or search engine ranking or if u change the file type, you need to change many code within your application.so in this situation, come to the second solution. 😉
The way is:
You need to add some command in your .htaccess file like below:
For html file :
AddType application/x-httpd-php .html
Or for .htm
AddType application/x-httpd-php .htm
If you only plan on including the PHP on one page, it is better to setup this way:
<Files myhome.html> AddType application/x-httpd-php .html </Files>
Example:
Now you can put php script like below in your html file :
<p id='php_cont'><?php echo "hi all"; ?></p>
N.B. Always be careful about your .htaccess file. if you have already one, just add the above command to the file, don’t replace all. and for beginner, the .htaccess file will be in your root folder, where your application or project exists (i.e. http://localhost/myapp)
That’s all. For very beginners it may be helpful and i think this is a common question for them that ‘How to use php in html file?’. cheers 😉