HTML5 and CSS3 All-in-One For Dummies
Book image
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:


Getting the Time, PHP Style

Date: "; print date("m-d"); print " n"; print "

Time: "; print date("h:i"); print "

"; ?>

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:


Getting the Time, PHP Style

Date: 07-05

Time: 03:50

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 taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: