Home

How to Use PHP with HTML5 Programming

|
Updated:  
2016-03-26 13:13:48
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

PHP is a different language than HTML5, but the two are very closely related. It may be best to think of PHP as an extension that allows you to do things you cannot do easily in HTML.

Every time you run getTime.php, it generates the current date and time and returns these values to the user. This would not be possible in ordinary HTML because the date and time (by definition) always change. While you could make this page using JavaScript, the PHP approach is useful for demonstrating how PHP works. First, take a look at the PHP code:

image0.jpg
<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 <title>showDate.php</title>
 </head>
 <body>
 <h1>Getting the Time, PHP Style</h1>
 <?php
print "<h2>Date: ";
print date("m-d");
print "</h2> n";
print " <h2>Time: ";
print date("h:i");
print "</h2>";
 ?>
 </body>
</html>

Embedding PHP inside HTML

The PHP code has some interesting characteristics:

  • It’s structured mainly as an HTML document. The doctype definition, document heading, and initial H1 heading are all ordinary HTML. Begin your page as you do any HTML document. A PHP page can have as much HTML code as you wish. (You might have no PHP at all!) The only thing the PHP designation does is inform the server that PHP code may be embedded into the document.

  • PHP code is embedded into the page. You can switch from HTML to PHP with the tag. Signify the end of the PHP code with the ?> symbol.

  • The PHP code creates HTML. PHP is usually used to create HTML code. In effect, PHP takes over and prints out the part of the page that can’t be created in static HTML. The result of a PHP fragment is usually HTML code.

  • The date() function returns the current date with a specific format. The format string indicates how the date should be displayed.

  • The result of the PHP code will be an HTML document. When the PHP code is finished, it will be replaced by HTML code.

View the results

If you view showDate.php in your browser, you won’t see the PHP code. Instead, you’ll see an HTML page. It’s even more interesting when you use your browser to view the page source. Here’s what you’ll see:

<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 <title>showDate.php</title>
 </head>
 <body>
 <h1>Getting the Time, PHP Style</h1>
 <h2 id="tab3" >Date: 07-05</h2>
 <h2 id="tab4" >Time: 03:50</h2>
 </body>
</html>

The remarkable thing is what you don’t see. When you look at the source of showDate.php in your browser, the PHP is completely gone! This is one of the most important points about PHP: The browser never sees any of the PHP. The PHP code is converted completely to HTML before anything is sent to the browser.

This means that you don’t need to worry about whether a user’s browser understands PHP. Because the user never sees your PHP code (even if he views the HTML source), PHP code works on any browser, and is a touch more secure than client-side code.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].