How to Use PHP with HTML5 Programming
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.
<!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 <?php 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>Date: 07-05</h2> <h2>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.