When you learn PHP, HTML tends to be out of the picture. It is an after-thought at best because we tend to focus on the nitty-gritty of PHP without being concerned about how to make it work with HTML. But PHP can be used to spice up static HTML pages (assuming you configured Apache to recognise PHP). Here you can see how to dynamically adapt HTML content as you continue to learn PHP.
You will recall from the basics of PHP syntax that every PHP script starts and ends with an angled bracket and a question mark. You also need to indicate that it is to be understood as PHP by including that abbreviation after the opening symbols:
<?php
some PHP code here
?>
When embedding PHP in HTML, we simply drop that PHP code into the HTML structure where we want the results to appear. Take the following HTML page as an example:
<html>Note that this will only work if you configure Apache for PHP. If you encounter problems, ensure that you have configured Apache and PHP correctly.
<head>
<title>The Meaning of Life</title>
</head>
<body>
<h1>The Meaning of Life</h1>
<div>
<p>
The dolphins say the meaning of life is
<?php
echo 9*3;
?>
</p>
</div>
</body>
</html>
As seen in the above sample, PHP is great for block-by-block building of web pages. The answer to the question appears on the same line as the rest of the text because that is where the PHP lies in the structure of the HTML. Alternatively, we can off-set the answer by putting it in a structure of its own:
<html>Note that we here are using the deprecated font tag for the sake of simplicity. Better is to specify font attributes in a CSS file.
<head>
<title>The Meaning of Life</title>
</head>
<body>
<h1>The Meaning of Life</h1>
<div>
<p>
The dolphins say the meaning of life is:
<center><strong><font size="7" color="red">
<?php
echo 9*3;
?>
</font></strong></center>
</p>
</div>
</body>
</html>
Having seen how to embed PHP in HTML, it is easier to imagine ways of applying PHP throughout your website. Go on now to learn PHP's keywords. Guides for other parts of the language are found below.
PHP Tutorials:
Apache Tutorials:
Install Apache | Basic Configuration | Apache In-Depth |
Recommend Web Hosting | Customising Apache | PHP Hosting |
Install MySQL | Basic MySQL Configuration | MySQL Configuration In-Depth |