How to Convert HTML to PHP

Written by c.a. rubino
  • Share
  • Tweet
  • Share
  • Email

PHP is a scripting language used by Web developers to add dynamic functionality and interactivity beyond what is available using a simple markup language such as HTML. PHP isn't actually seen by a Web browser. Instead, it relies on HTML to send and display the data it generates. PHP can exist side-by-side to HTML code, or it can be used to generate the code dynamically on the fly. Converting an existing HTML document to PHP code involves structuring the PHP code blocks and writing PHP script to generate lines of HTML.

Skill level:

Other People Are Reading


  1. 1

    Rename the HTML file. If the file contains the extension of ".html" or ".htm," the Web server will attempt to interpret the file as pure HTML unless the server has been specially configured. Using Windows, rename the HTML file so it ends in ".php" and the browser will correctly identify it.

  2. 2

    Embed the HTML within the PHP code. PHP and HTML can easily exist within the same file. In most cases no modification needs to be done to the HTML code. At the very beginning of the file enter "<?php" to begin the PHP code block. Inside the block include whatever PHP code you wish. When you are ready to begin HTML code again, enter "?>" to close the block. You can easily switch back and forth between blocks of PHP or HTML code as many times as needed throughout the file.

  3. 3

    Use the Echo function to have PHP generate HTML code. If you need portions of your HTML code to be generated by PHP -- to display the value of a variable, for example -- you will need to use Echo to generate the HTML. While you are within a PHP code block, type (Echo ") to begin the Echo command. Enter you HTML code however you want. If you need to insert a variable, break out of the echo with a (".), insert the variable name ($_VARIABE) and re-enter the Echo (."). When you are finished with the Echo, use (";) to close the function. An example of an Echo function will look like "Echo (Hello my name is ".$_Name .";" without the quotation marks).

Tips and warnings

  • PHP files that generate Web pages using HTML code need to contain all the necessary code for standalone HTML files such as the header information.
  • Files containing PHP code must be located on a Web server with PHP installed for it to display properly. Running a PHP file directly from your hard drive will have no effect.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.