The If Conditional

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.

graphics/06sc01.gif

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:

  1. Open numbers.php (Script 6.1) in your text editor.

  2. Change the title (line 3) to <TITLE> Conditionals</TITLE> (Script 6.2).

    Script 6.2. Frequent use of if conditionals makes your programming more professional by establishing particular parameters before going through corresponding processes. Here you'll insure that a $Quantity was received before proceeding with the calculations.

    graphics/06sc02.jpg

  3. Change lines 1023 to be the result of an if conditional.

     if ($Quantity) {     $Quantity = abs($Quantity);     $Discount = abs($Discount);     $Tax++; // $Tax is now worth   1.06.    $TotalCost = (($Cost *     $Quantity) - $Discount) * $Tax;    $Payments = round ($TotalCost, 2)   / 12;    // Print the results.    print ("You requested to purchase   $Quantity widget(s) at $$Cost   each.\n<P>");    print ("The total with tax,   minus your $$Discount, comes   to $");    printf ("%01.2f", $TotalCost);    print (".\n<P>You may purchase   the widget(s) in 12 monthly   installments of $");    printf ("%01.2f", $Payments);    print (" each.\n<P>"); } 

    In PHP, simply using a variable name as your condition (as you have here with $Quantity) is the same as saying, "If the variable $Quantity exists (i.e., has a value other than zero)." Here you are telling the PHP to execute the following lines only if $Quantity has a value that's not zero. In order words, the calculations will only be made if there is a quantity to use.

  4. Save your script, upload it to the server, and test the page in your Web browser both with and without the requisite $Quantity information (Figures 6.1 and 6.2).

    Figure 6.1. As long as the page receives a value for the $Quantity variable, it will proceed as it had prior to adding the if conditional.

    graphics/06fig01.gif

    Figure 6.2. Thanks to the if conditional, your Web site will never produce ugly results like this again.

    graphics/06fig02.gif

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


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net