Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [201]

By Root 1372 0

//I’m back in PHP

?>

This option (switching back and forth) is generally used when you have a lot of XHTML code with only a few simple PHP variables. I prefer the heredoc approach, but feel free to experiment and find out what system works for you.

Printing shortcut

When switching in and out of PHP, if you have just one variable you want to print, depending upon your server setup, you may be able to do print the variable like this:

You don’t have to actually write print when using this technique. Note that this trick doesn’t work if you have to type php after the question mark in the opening PHP tag.

Chapter 2: PHP and XHTML Forms

In This Chapter

Understanding the relationship between XHTML and PHP

Using the date() function

Formatting date and time information

Creating XHTML forms designed to work with PHP

Choosing between get and post data transmission

Retrieving data from your XHTML forms

Working with XHTML form elements


PHP is almost never used on its own. PHP is usually used in tight conjunction with XHTML. Many languages have features for creating input forms and user interfaces, but with PHP, the entire user experience is based on XHTML. The user never really sees any PHP. Most of the input to PHP programs comes from XHTML forms, and the output of a PHP program is an XHTML page.

In this chapter, you discover how to integrate PHP and XHTML. You explore how PHP code is embedded into XHTML pages, how XHTML forms can be written so they will send information to a PHP program, how to write a PHP program to read that data, and how to send an XHTML response back to the user.


Exploring the Relationship between PHP and XHTML

PHP is a different language than XHTML, 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 XHTML. See Figure 2-1 for an example.

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 XHTML 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:

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

showDate.php

Getting the Time, PHP Style

print ”

Date: ”;

print date(”m-d”);

print ”

\n”;

print ”

Time: ”;

print date(”h:i”);

print ”

”;

?>

Figure 2-1: This program gives me the current date and time.

Embedding PHP inside XHTML

The PHP code has some interesting characteristics:

♦ It’s structured mainly as an XHTML document. The doctype definition, document heading, and initial H1 heading are all ordinary XHTML. Begin your page as you do any XHTML document. A PHP page can have as much XHTML 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 XHTML to PHP with the symbol.

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

♦ The date() function returns the current date with a specific format. The format string indicates how the date should be displayed. (See the sidebar “Exploring the date() format function,” in this chapter, for more information about date formatting.)

♦ The result of the PHP code will be an XHTML document.

Return Main Page Previous Page Next Page

®Online Book Reader