HTML PHP

by Vincy. Last modified on September 27th, 2022.

Like other server-side scripting languages, PHP scripts are running on the server. And then, the server will send an HTML response after executing PHP scripts. By adding HTML markup to a PHP script, we can receive server responses 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, learn the various ways of using PHP scripts in HTML.

HTML in PHP Page

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

Adding HTML outside PHP delimiters

This is simply by separating HTML code from the PHP script. And, it is a 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 examples 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, the PHP script is separated from HTML. It reads HTML form elements from the 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 help to print HTML content into the browser. In the above example also, we have used this method on providing HTML breaks and bold tags.

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

Not only that, but we can also print the 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 with this, you can cut the HTML content from the first example and save it with a .html extension. And then, importing this newly saved HTML file will make no change in the 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 as follows.

<?php
require_once ("external_html_content.html");
?>

PHP in HTML

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

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

<?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 a .php extension. But, we can configure an HMTL page to read PHP content by using a .htaccess file. And, these htaccess settings will differ from the web server configuration.

Download HTML PHP Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page