Embedding PHP code in Web Pages

To make the HTML pages interactive, developers have a variety of scripting languages to choose from. PHP has been a preferred scripting language due to its ease of learning, ability to connect with open source databases like MySQL and MonoDB. PHP code library is an immense resource for developers to create dynamic web applications with least efforts and high security. As a developer when you graduate from creating simple HTML web pages to complex function oriented web applications, you will need embedding PHP into HTML. This is not as complicated as it may look right now. PHP is designed to seamlessly integrate within HTML by embedding PHP scripts of any size.

When a dynamic page has to be developed using HTML code, it has to be saved with .php extension. A PHP file could be a mix of HTML, CSS, JavaScript and PHP sections or an all PHP script file that can be called upon an action taken by a user on a pure HTML file.

How PHP script is processed?

When you are embedding PHP script in an HTML code, you need to save the file in the file system of your web server like Apache. In the current case you need to save your php or HTML file in WAMP/bin folder.

When a user requests the server for this page by typing the URL, the server responds back with output generated as HTML code after processing the PHP code. PHP code is never sent to the users’ browser.

You can embed the PHP script anywhere in an HTML code and any number of times. Server processes only those scripts lines that are enclosed in <?php ?> tags. Rest of the code is ignored by the PHP interpreter at the web server and returned as it is to the user’s web browser.

Ways for embedding PHP scripts in HTML

XML Style

This is also the preferred style of embedding PHP code in an HTML file since XML is widely accepted as the next and convenient successor of HTML dues to ability to define your own tags. The desired PHP statements are enclosed in PHP tags as given in example.

echo “This is my first PHP page”;


This code is saved as first.php file in WAMP/bin folder. In the browser the file is executed by typing “\\localhost\first.php”. The message “This is my first PHP page” will be displayed on the screen.

Short Tag Style

Another way to write PHP scripts in HTML is to use the short tag that do not require writing “php”

<? echo "This is my first PHP page"; ?>

This is another version of the PHP file written in earlier example works perfectly with short tag. The short tag style of writing the PHP scripts in HTML is very convenient. It also improves the readability of code.  This flexibility allows you to dynamically generate the title of your webpage by using the variable getting value after a user interaction as shown in the below example

 <html><head><title><?echo $dynTitle?></title></head>


In this example the PHP variable is assigned value and the user will see the contents of this variable as title of the displayed page.

Using SCRIPT Tag

You are aware that the SCRIPT tag in HTML can be used for embedding any kind of script like JavaScript or VBScript. The same tag can also be used to embed the PHP scripts as well. Developers prefer this style when they need to create dynamic web pages with portability of various platforms.

 <HTML><script language="php" > echo " This is my first PHP page ";</script></HTML>

ASP style

The short tag style and the XML tag style is not the accepted HTML code. Some HTML editors may fail to parse it to present for color highlighting and context-sensitive help. If you are comfortable in editing your web page codes on these modern day editors than it will be better to embed your PHP codes in ASP(Active Server pages) style since most of these editors recognise it and parse it correctly for presentation

The ASP-style embedding is the same as the short tag style where you have to use % in place of ?.

 <HTML><% echo "This is my first PHP page "; %></HTML>