Comparison

I l @ ve RuBoard

When I first introduced the assignment operator (the equal sign), in Chapter 2, Variables, I was careful to explain that its meaning is not exactly what you would conventionally think it would be. In the line $Variable = 5; you are not stating that $Variable "is equal to" 5 but that it "is set to the value of" 5.

When writing conditionals, you will often want to see if a variable is equal to a specific value (to match user names or passwords, perhaps), which you cannot do with the equal sign alone (since that is for assigning a value not equating values). For this purpose you have the equal operator (==), created by using two equal signs together.

 $Variable = 5; $Variable == 5; 

Using these two lines of code together first establishes the value of $Variable as 5, then makes a TRUE statement when seeing if $Variable is equal to 5. This demonstrates the significant difference one more equal sign makes in your PHP code and why you will want to distinguish carefully between the assignment and comparison operators.

Not equal to, in PHP, is represented by an exclamation mark coupled with an equals sign (!=). In fact, the exclamation point, in general, means "not." So as $Variable means "$Variable exists and has a value (other than zero)", !$Variable is the way of saying "$Variable does not exist and has no value (or a value of zero)."

The remaining comparison operators are identical to their mathematical counterparts: less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=).

In order to add functionality to your widget calculator, you'll rewrite the numbers .php script so that it only applies a discount for a sale of greater than $50.

To use comparison operators:

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

  2. Change the cost of the widget to a much smaller number so that a $50 limit is less easily reached (Script 6.3 line 8).

     $Cost = 20.00; 
    Script 6.3. The comparative operators like the less than or equal to (<=) here allow you to specify better numeric conditions in your code.

    graphics/06sc03.jpg

  3. After the $Tax++; line (line 13), change the $TotalCost equation to read:

     $TotalCost = ($Cost * $Quantity); 

    Since the discount is only going to apply if the total is over $50, you should first calculate the total separately.

  4. Create an if conditional checking to see if the value of the sale is over $50.

     if ($TotalCost >= 50) {         $TotalCost = $TotalCost -          $Discount;    } 

    Using comparative operators within the condition of the if conditional, you can specify that the discount will only be applied if the $TotalCost value is lesser than or equal to $50 (don't write the dollar sign in your condition). The subtraction of $Discount is the statement that is executed if the condition is TRUE. If the condition is FALSE, no discount will be subtracted.

  5. Now apply the tax to the total cost.

     $TotalCost = $TotalCost * $Tax; 
  6. The rest of this script is unchanged from before, including the determination of the monthly payments and printing all of the results.

  7. Save your script, upload it to the server, and test in your Web browser using different $Quantity values (Figures 6.3 and 6.4).

    Figure 6.3. If you do the math you'll see that the discount is not applied because the total was less than $50. Compare this with the math in Figure 6.4.

    graphics/06fig03.jpg

    Figure 6.4. Now that the user has ordered enough widgets to push the total over $50, the discount has been applied.

    graphics/06fig04.jpg

Tip

In an if conditional, if you make the mistake of writing $Variable = 5 as opposed to $Variable == 5, you'll see that the corresponding conditional statements will always be executed. This is because, while the condition $Variable == 5 may or may not be TRUE, the condition $Variable = 5 will always be 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