PHP for the World Wide Web (Visual QuickStart Guide)
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:
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 |