How To Use HTML and PHP On The Same Webpage

How To Use HTML and PHP On The Same Webpage
One of the features that make PHP such a popular language is that you can mix your HTML and PHP code together on the same page. There are just two simple rules you need to remember. You will need to change the extension on the end of the name of the webpage. Secondly, wrap your PHP code inside the opening and closing PHP tags.

  • Webpage Extension

    You will need to change the extension on the name of your webpage from .html to a PHP extension. Your web host can tell you which PHP extension to use for the server you are on. First check the online documentation. If the answer is not there you can email your web host. Usually the extension will be either .php or .php3.

    Example: mywebpage.html ------> mywebpage.php3


  • Opening and Closing PHP Tags

    You will enclose all of your PHP code between opening (<?php) and closing (?>) tags. This is how the server can tell the HTML and the PHP code apart.

OK, now we are ready to create a webpage that contains both PHP and HTML. First you will want to code the top part of the webpage using HTML. Open you text editor and add this code.

<html>
<head>
<title>Sample 2</title>
</head>
<body>

Next we will add some PHP code that will print either "Good Morning" or "Good Evening" depending on the time of day that the webpage is viewed. If the hour of the day is before noon, then "Good Morning" will be printed. If the time is noon or later, then "Good Evening" will be printed. I know we haven't discussed the PHP code in detail, but take a look at it anyway. I bet you can figure out how it works. Then add the PHP code to the webpage.

<?php
$current_time = date(G);
if ($current_time < "12") {echo "Good Morning<br>";}
else {echo "Good Evening<br>";}
?>

Finally add the code below to finish the rest of the message and the end of the webpage using HTML. Click here to view the entire code.

Welcome to my website.

</body>
</html>

Now save your webpage. Call it "sample2.php3" (or sample2.php) and put the file in the following location.

C:\sokkit\site\sample2.php3

Finally we will test the webpage. Open the Sokkit Control Panel and RIGHT click on the View Site button. Choose "Open in New Window" from the pop-up list. This will cause the sokkitdefault.html webpage to be displayed in the new window. You should see a message saying "This site is powered by Sokkit 3.4". Now go to the url address line in the web browser. It should say https://localhost/. To test your webpage, add sample2.php3 at the end of this and click Enter. You will see a white page with the following.

Good Morning (Good Evening)
Welcome to my website.






This site needs an editor - click to learn more!



RSS
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.