Combine PHP and HTML

In this practical lesson, we will see how you can combine PHP code with common HTML. In fact, PHP is an HTML-Embedded Language, that is, it allows you to combine both PHP instructions and simple HTML code within the same file.

Let’s take an example of a “mixed” .php page consisting of both PHP and HTML.

<html>
<head>
<title>PHP and HTML</title>
</head>
<body>

This is <b>HTML</b>.

<?php
echo "This is php PHP!";
?>

</body>
</html>

Obviously the page in question will be saved with the extension .php (and not .html).

Let’s take another example that also exploits variables. For convenience, we take the example seen in the previous lesson.

<?php
$height = 4;
$width = 6;
$area = $height * $width;
?>

<html>
<head>
<title>PHP and HTML</title>
</head>
<body>

Height: <?php echo $height;?><be>
Width: <?php echo $width;?><be>
Area: <?php echo $area;?>

</body>
</html>

As you can see, after defining the value of the variables inside a portion of PHP code (which we placed at the beginning of the document), we “printed” them inside the HTML document using a simple syntax:

<?php echo $variable; ?>

Leave a Comment