How to convert html page to PHP page

I have a website which is built in HTML5 but now i want to change it with PHP, can anyone suggest me how to do this.

Regards Samith


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

You can use an online converter i.e. is the address to one I found.

if you got 1000+ html files or any files and you want to change the extension [example: your-answer.html to your-answer.php] Email me but at cost


There are 2 ways to convert your HTML page to PHP. The first one is easier, but will not work in any situation. The other one is little more complicated, but gives you better control over the conversion.

Incorporating HTML directly in the PHP file.

This is the easy way. In every PHP file the PHP code must be inserted between <?php and ?> tags. The rest will be interpreted by the web server as pure HTML. So, if you just save your HTML page with .php extension, that’s all, you have the html code “converted”. Let say the php file below is named “example.php”:

Original file (example.html)

PHP file (example.php)

<html> <head></head> <body> This is HTML code </body> </html>

<html> <head></head> <body> This is HTML code <?php echo “This is PHP code”; ?> </body> </html>

In the example above, we just put the php code (in red) inside the body. The web server will know that everything between <?php and ?> is php code and will execute it, the rest will be sent directly to the browser, because it is just static HTML. So, you can “convert” every html page to php just by renaming the file with .php extension. Unfortunately, in many cases, this is not the solution that works. Dropping in and out of parsing mode can be really slow. That’s why we may use another way to insert html code in php file.

Using PHP commands (echo, print etc.)

We can send HTML code to browser directly within PHP code. The most used way to do this is with the “echo” command. We just put the html code inside the echo command and that’s all. One simple example will look like this:

Original file (example.html)

PHP file (example.php)

<html> <head></head> <body> This is HTML code </body> </html>

<?php echo “<html>\n”; echo “<head></head>\n”; echo “<body>\n”; echo “This is HTML code\n”; echo “</body>\n”; // some PHP code here … echo “</html>\n”; echo “\n”; ?>

Such conversion method gives you better control and more options, but it is really tedious. (That’s why I build the HTML to Script Converter) But for small-to-medium html output you should use this instead of escaping php, writing the html and going back to php. Switching many times between php/html is slow method. (However – for big html pieces, it is faster than echo or print)

echo vs. print vs. printf

Both echo() and print() are php methods for text /html/ output, but print() is slower than echo(). The reason for that is that print function will return a status indicating if it was successful or not, while echo doesn’t return anything. So, in most cases, echo is the right choice.

Printf is more powerful function, and that’s why it is slower. We should use printf() just in case that we really need its functionality.


When we want to put medium-size or big portion of HTML code into PHP script, we should close the PHP code ( ?> ), put the raw HTML code and then continue with opening PHP tag ( <?php ). For big portions of html this is faster

For smaller pieces of HTML code or if we need many PHP snippets into the HTML, the better choice is to convert and put the code into “echo” commands, because switching on and off the PHP parser will slow-down the code.