Specifications
Using PHP
P
ART I
10
This chapter gives you a quick overview of PHP syntax and language constructs. If you are
already a PHP programmer, it might fill some gaps in your knowledge. If you have a back-
ground using C, ASP, or another programming language, it will help you get up to speed
quickly.
In this book, you’ll learn how to use PHP by working through lots of real world examples,
taken from our experience in building e-commerce sites. Often programming textbooks teach
basic syntax with very simple examples. We have chosen not to do that. We recognize that
often what you want to do is get something up and running, to understand how the language is
used, rather than ploughing through yet another syntax and function reference that’s no better
than the online manual.
Try the examples out—type them in or load them from the CD-ROM, change them, break
them, and learn how to fix them again.
In this chapter, we’ll begin with the example of an online product order form to learn how
variables, operators, and expressions are used in PHP. We will also cover variable types and
operator precedence. You will learn how to access form variables and how to manipulate them
by working out the total and tax on a customer order.
We will then develop the online order form example by using our PHP script to validate the
input data. We’ll examine the concept of Boolean values and give examples of using if, else,
the ?: operator, and the switch statement.
Finally, we’ll explore looping by writing some PHP to generate repetitive HTML tables.
Key topics you will learn in this chapter include
• Embedding PHP in HTML
• Adding dynamic content
• Accessing form variables
• Identifiers
• User declared variables
• Variable types
• Assigning values to variables
• Constants
• Variable scope
• Operators and precedence
• Expressions
• Variable functions
• Making decisions with if, else, and switch
• Iteration: while, do, and for loops
03 7842 CH01 3/6/01 3:39 PM Page 10