PHP for the World Wide Web (Visual QuickStart Guide)
I l @ ve RuBoard |
The basic programming conditional is the standard if (what used to be called an if-then conditionalthe then is now implied ). The syntax for this kind of conditional is very simple: if (condition) { statement(s); } Script 6.1. In the original numbers .php script, the calculations were made and the results printed based upon the assumption that the $Quantity and $Discount values were received, which is not a good programming practice.
The condition must go within parentheses and then you begin the statements area after a curly brace. The statements section of the conditional is where executable commands are placed (for example, printing a string, adding two numbers together, and so forth). Each separate statement (or command) must have its own semicolon indicating the end of the command line, but there is no limit to how many statements you write as the result of a conditional. You then close the statements section with another curly brace . Commonly programmers put these statements indented from the initial if line to indicate that they are the result of a conditional but that is not syntactically required. Failure to use a semicolon after each statement, forgetting an opening or closing parentheses or curly brace, or using a semicolon after either of the braces will all cause errors to occur. PHP uses the concepts of TRUE/FALSE when determining whether or not to execute the statements. If the condition is TRUE, the statements will be executed; if FALSE, they will not be. The next section, More Operators, goes into TRUE/FALSE in more detail. To begin working with the if conditional, you'll rewrite the calculator page from Chapter 4, Using Numbers, so it works only if the necessary quantity has been submitted. This will prevent calculations from being made without all the requisite data, which could cause illogical and erroneous Web page results. To create an if conditional:
Tip If the statement area of your conditional is only one line long, you technically do not need the curly braces. If that is the case, you can actually place the whole conditional on one line like so: if (condition) statement; I mention this fact here as you may run across code in this format. However, I would suggest that you always use the multi-line format (as demonstrated in the syntax introduction above) to improve consistency and minimize errors.
Tip You can nest if conditionalsplacing a second if as the result of a prior conditionwithout a problem in PHP as long as you remember to close your conditionals appropriately.
Tip Later in the chapter (see Using Else), you'll learn how to execute commands if a condition isn't met. Then, if a necessary value isn't received, you could program the script to specifically request it in order to proceed.
Tip You can also use the isset() function to determine if a variable exists. Unlike simply referring to a variable by name as you have done here, isset() will return TRUE if the variable is equal to zero. For example: $Quantity = 0; if ($Quantity) { ... // FALSE if (isset($Quantity)) { ... // TRUE |
I l @ ve RuBoard |