Combine php and html code

This chapter is from the book

Combining HTML and PHP Code on a Single Page

In some circumstances, you may want to include form-parsing code on the same page as a hard-coded HTML form. Such a combination can be useful if you need to present the same form to the user more than once. You would have more flexibility if you were to write the entire page dynamically, of course, but you would miss out on one of the great strengths of PHP. The more standard HTML you can leave in your pages, the easier they will be for designers and page builders to amend without reference to you. You should avoid scattering substantial chunks of PHP code throughout your documents, however. This will make them hard to read and maintain. Where possible you should create functions that can be called from within your HTML code, and can be reused in other projects.

For the following examples, imagine that we are creating a site that teaches basic math to preschool children and have been asked to create a script that takes a number from form input and tells the user whether it is larger or smaller than a predefined integer.

Listing 9.8 creates the HTML. For this example, we need only a single text field, but even so, we'll include a little PHP.

Listing 9.8 An HTML Form that Calls Itself

 1: 
 2: 
 3: Listing 9.8 An HTML form that calls itself
 4: 
 5: 
 6: 
7: Type your guess here: 8:
9: 10:

Whatever we name the page that contains this form, the fact that we have left out the action attribute of the form element will mean that the form will be submitted back to its own url.

NOTE

Almost all browsers will submit a form to its current page if the form element's action attribute is omitted. You can, however, explicitly tell the browser to submit a form back to its own document by using the predefined $PHP_SELF variable.

The script in Listing 9.8 will not produce any output. In Listing 9.9, we begin to build up the PHP element of the page. First, we need to define the number that the user will guess. In a fully working version, we would probably randomly generate this, but for now we will keep it simple. We assign '42' to the $num_to_guess variable on line 2. Next, we need to decide whether the form has been submitted; otherwise, we will attempt to assess variables that have not yet been made available. We can test for submission by testing for the existence of the variable $guess. $guess will have been made available as a global variable if your script has been sent a "guess" parameter. If this isn't present, we can safely assume that the user has arrived at the page without submitting a form. If the value is present, we can go ahead and test the value it contains. The test for the presence of the $guess variable takes place on line 4.

Listing 9.9 A PHP Number Guessing Script

 1:  $num_to_guess )
 7:  $message = "$guess is too big! Try a smaller number";
 8: elseif  ( $guess < $num_to_guess )
 9:  $message = "$guess is too small! Try a larger number";
10: else // must be equivalent
11:  $message = "Well done!";
12: 
13: ?>
14: 
15: 
16: Listing 9.9 A PHP number guessing script
17: 
18: 
19: 

20: 21:

22: 23: Type your guess here: 24: 25: 26:

The bulk of this script consists of an if statement that determines which string to assign to the variable $message. If the $guess variable has not been set, we assume that the user has arrived for the first time and assign a welcome string to the $message variable on line 5.

Otherwise, we test the $guess variable against the number we have stored in $num_to_ guess, and assign advice to $message accordingly. We test whether $guess is larger than $num_to_guess on line 6, and whether it is smaller than $num_to_guess on line 8. If $guess is neither larger nor smaller than $num_to_guess, we can assume that it is equivalent and assign a congratulations message to the variable (line 11). Now all we need to do is print the $message variable within the body of the HTML.

There are a few more additions yet, but you can probably see how easy it would be to hand this page over to a designer. He can make it beautiful without having to disturb the programming in any way.

I learned PHP by hacking away at phpBB2, even submitting a few mods to their database, which others downloaded and used. (I don't believe phpBB2 is supported any more with phpBB3 out so long now, so the v2 mods database is no more.)

One of my favorite things about phpBB was their templates system, which let the editor completely separate the HTML and the PHP. PHP files contained PHP: logic, database queries, and activating the templates. TPL files contained templates: HTML, template variables, and specialized HTML comments to allow for conditional or repeating blocks.

However, any time I see someone's PHP code online, it's either a small snippet working with a single function or such, or the PHP is full of strings containing HTML (or worse, HTML with PHP interspersed). phpBB is the only PHP I've looked at which actually separates the language and the markup language, suggesting to me that few, if any, other PHP codebases do such a thing.

I'm looking to start working with some PHP again, but this time it won't be a phpBB forum, and it will be on a team. Based on my experience, separation of PHP and HTML is uncommon (please correct me if I'm wrong on this!). However, I'm so used to that dividing line, I hate reading mixed PHP and HTML.

In the "real world" of PHP programming, what's the preferred method:

  • PHP files with strings of HTML
  • HTML files broken up with PHP blocks
  • PHP and HTML completely separate (I wish?)
  • Something else?