Chapter 44. A Trivia Game

In this chapter, we present a full working example of a small PHP application: a web-based trivia game with a twist (the "Certainty Quiz"). The main virtue of the chapter is its completeness: Instead of using code fragments to illustrate talking points, as we do in most other chapters, we're showing everything, soup to nuts. As a result, this is one of the larger examples in the book, weighing in at more than 1300 lines of PHP code.

Concepts Used in This Chapter

The code in this chapter uses a wide variety of techniques, tricks, and technologies that we've presented elsewhere in the book. In particular:

  • We make heavy use of the object-oriented features of PHP (Chapter 20).

  • We rely on PHP's session mechanism to propagate game data from page to page (Chapter 24).

  • We use a back end database (MySQL) to store questions and high scores (Part II).

  • We do some behind-the-scenes mathematics, including approximating nth roots (Chapters 9 and 27).

  • We use arrays for storing data and for manipulating data returned from the database (Chapters 8 and 21).

  • We do a lot of string processing and concatenation to build our display pages, including the heredoc technique for templating pages (Chapters 7 and 21).

  • We use the new exception-handling features of PHP5 to catch database and session problems (Chapter 31).

We highlight some of these ...

Get PHP6 and MySQL® 6 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.