Logical

I l @ ve RuBoard

Logical operators help you create reasonable constructsstatements that have a value of either TRUE or FALSE. In PHP, a condition is TRUE if it is simply a variable name and that variable has a value that is not zero (as you've seen already), such as:

 $Variable = 5; if ($Variable) {  ... 

A condition is also TRUE if it makes logical sense, like:

 if (5 >= 3) {  ... 

Your condition will be FALSE if it refers to a variable and that variable has no value or if you have created an illogical construct. The following conditional will always be false:

 if (5 <= 3) {  ... 

To go beyond simple one-part conditions, PHP supports six types of logical operators: two versions of and (AND or &&); two versions of or (OR or a character called the pipe put together twice); not (NOT); and or not (XOR). When you have two options for one operator (as with and and or ), they differ only in precedence. See Appendix C, PHP Resources, to view the table of operator precedence.

Using parentheses and logical operators, you can create even more in-depth conditionals for your if conditionals. For an AND conditional, every conjoined part must be TRUE. With OR, one subsection must be TRUE to render the whole condition TRUE. These conditionals are TRUE:

 if ( (5 <= 3) OR (5 >= 3) ) {  ... if ( (5 > 3) AND (5 < 10) ) {  ... 

These conditionals are FALSE:

 if ( (5 != 5) AND (5 > 3) ) {  ... if ( (5 != 5) OR (5 < 3) ) {  ... 

As you construct your conditionals, remember two important things: first, in order for the statements which are the result of a conditional to be executed, the conditional has to have a TRUE value; second, by using parentheses, you can ignore rules of precedence and insure that your operators are addressed in the order of your choosing.

To demonstrate logical operators, you should add another conditional to the numbers .php page letting the user know if their discount does not apply.

To use logical operators:

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

  2. After the $TotalCost value is first calculated (Script 6.3 line 14) but before the if ($TotalCost >= 50) conditional, add (Script 6.4):

     if ( ($TotalCost < 50) AND      ($Discount) ) {       print ("Your $$Discount      discount will not apply      because the total value      of the sale is under      !\n<P>");    } 

    This conditional will check for two things. First it will see if $TotalCost is less than $50. Second, it will determine if a non-zero $Discount value exists. If both of these are TRUE, the message will be printed. If at least one of these conditions is FALSE, the whole condition will also be FALSE (because the larger conditional is ruled by the logical AND operator) and therefore the message will not be printed.

    Script 6.4. In this script the logical operator AND establishes a specific condition under which the message will be printed. The AND requires that both sub-conditions are TRUE in order for the whole condition to be TRUE.

    graphics/06sc04.jpg

  3. Since those are the only changes to the script, you can now save your script, upload it to the server, and test in your Web browser (Figures 6.5 and 6.6).

    Figure 6.5. The widget calculator is slightly more professional now that it lets the user know that their discount will not apply to the sale.

    graphics/06fig05.jpg

    Figure 6.6. Even though the total sale value is less than $50, no message is printed to the user since $Discount does not have a value.

    graphics/06fig06.jpg

Tip

It is another common programming conventionwhich I've maintained in this bookto write the terms TRUE and FALSE in all capitals.


Tip

It is very easy in long, complicated conditionals to forget an opening or closing parentheses, which will either produce error messages or unexpected results. Find some system (like spacing out your conditionals as I have done) to help clarify your code.


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