HTML PHP

As like as other server side scripting languages, PHP scripts are running in server. And then, server will send HTML response after executing PHP scripts. By adding HTML markup with a PHP script, we can receive server response with more readability.

Sometimes, we can use PHP scripts as the value of HTML tag attributes. So, this article will help to learn about how to embed HTML into a PHP page, and also, to learn the various ways of using PHP scripts into HTML.

html_php

HTML in PHP Page

The following ways are generally used for embedding HTML code into a PHP page.

Adding HTML outside PHP delimiters

This this simply by separating HTML code from PHP script. And, it is widely used way of HTML embedding. We are also familiar with this method of adding HTML, while seeing examples for the tutorials PHP login, PHP contact page and etc.

Though we have seen many example in various articles, let us have one more as follows.

<?php
print "<b>Getting HTML Form values</b><br/>";
if(count($_GET)>0) {
$title = $_GET["title"];
print $title;
}
?>
<form name="frmTitle">
<tr>
<td>Title</td>
<td>
<input type="text" name="title" />
<input type="submit" value="Submit" />
</td>
</tr>
<tr>
</form>

In the above program, PHP script is separated from HTML. It reads HTML form elements from PHP $_GET request method and prints accordingly. And, we can also embed HTML content between two separate PHP blocks.

Specifying HTML with PHP printing statements

On the other hand, PHP output statements helps to print HTML content into the browser. In the above example also, we have used this method on providing HTML breaks and bold tags.

print "<b>Getting HTML Form values</b><br/>";
...

Not only that, we can also print above HTML form content by using PHP print statements. For example,

<?php
echo '<form name="frmTitle">';
echo '<tr>';
echo '<td>Title</td>';
echo '<td>';
echo '<input type="text" name="title" />';
echo '<input type="submit" value="Submit" />';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '</form>';
?>

Using PHP include/require to import HTML files

By using any one of PHP functions include(), require(), include_once() or require_once(), we can import external HTML files into a PHP page.

For experimenting this, you can cut the HTML content from the first example and save it with .html extension. And then, importing this newly saved HTML file, will make no change in display.

So, let us save the following content as external_html_content.html.

<html>
<head>
<title>Importing HTML</title>
</head>
<body>
<form name="frmTitle">
<tr>
<td>Title</td>
<td>
<input type="text" name="title" />
<input type="submit" value="Submit" />
</td>
</tr>
<tr>
</form>
</body>
</html>

And then, we can import this file as shown in the PHP code follows.

require_once("external_html_content.html");

PHP in HTML

Similarly, we can use PHP scripts in between HTML code. Some of the such example scenarios are listed here.

Let us have an example for adding PHP scripts in between HTML form field to pre-populate recently submitted form value.

<?php
print "<b>Getting HTML Form values</b><br/>";
if(count($_GET)>0) {
$title = $_GET["title"];
print $title;
}
?>
<form name="frmTitle">
<tr>
<td>Title</td>
<td>
<input type="text" name="title" value="<?php if(isset($_GET['title'])) { echo $_GET['title']; } ?>" />
<input type="submit" value="Submit" />
</td>
</tr>
<tr>
</form>

And note that, for using PHP script with HTML content, the file should have .php extension. But, we can configure a HMTL page to read PHP content by using .htaccess file. And, this htaccess settings will be differed with the web server configuration.

Download HTML PHP Source Code

This HTML code tutorial was published on September 12, 2013.

↑ Back to Top