Friday, 25 August 2017

PHP code within HTML file

Hello learners!

Today, the first step towards server side scripting is PHP. The PHP is very popular web scripting language which execute on server. So any web server will be required to test PHP scripts. PHP can also be written within HTML file. But it won't work without some configurations.

Students are facing problems when they write php code within html file(.html extension) and try to check output. They will get all html output, but not php. If they save file with .php extension, then it works fine.

 Example code

Output with only html code

Mostly we use xampp or wampp (with apache server) to run PHP scripts. So let me show you how to configure server to run php code within html file.

If you are using xampp, open the file
C:\xampp1\apache\conf\extra\httpd-xampp.conf
You can open this file in any text editor as it is plain text file. When you open it, you will find some code as following screen shot,



See, there is only php extensions' entries. As we want to support html extension for php code, we need to create html's entry also. So copy 1st block of FilesMatch tag and paste as 3rd block. After paste, change the extension string from .php to .html.  Check the following image,

conf file after edit.


Now, save the file and if apache is runnig, restart it. Now check the output again in browser. The html and php both output will be there.

Output after configuration.


Notes: Do at your risks. Backup your files before making any changes.