How to convert html page to PHP page

December 22, 2015 11.2k views
PHP Frameworks PHP

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

3 Answers

Hi!

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.

Conclusion

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.

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 info@google.or.ke but at cost

You can use an online converter i.e. http://www.andrewdavidson.com/convert-html-to-php is the address to one I found.

Have another answer? Share your knowledge.