Web hosting compare - 85Chapter 6Control and FunctionsThe &&and ||operators will be
85Chapter 6Control and FunctionsThe &&and ||operators will be familiar to C programmers. The !operator is usually callednot, since it negates the argument it operates on. As an example of using logical operators, consider the following expression: (($statement_1 && $statement_2) || ($statement_1 && !$statement_2) || (!$statement_1 && $statement_2) || (!$statement_1 && !$statement_2)) This is a tautology, meaning that it is always true regardless of the values of the statementvariables. There are four possible combinations of truth values for the two variables, each of which is represented by one of the &&expressions. One of these four must be true, andbecause they are linked by the ||operator, the entire expression must be true. Here s another, slightly trickier tautology using xor: (($statement_1 and $statement_2 and$statement_3) xor((!($statement_1 and $statement_2)) or(!($statement_1 and $statement_3)) or(!($statement_2 and $statement_3)))) In English, this expression says, Given three statements, one and only one of the followingtwo things hold either 1) all three statements are true, or 2) there are two statements thatare not both true. Precedence of logical operatorsJust as with any operators, some logical operators have higher precedence than others, although precedence can always be overridden by grouping subexpressions using parenthe- ses. The logical operators listed in declining order of precedence are: !, &&, ||, and, xor, or. Actually, and, xor, and orhave much lower precedence than the others, so that the assign- ment operator (=) binds more tightly than and, but less tightly than &&. A complete table of operator precedence and associativity can be found in the online manualat www.php.net. Logical operators short-circuitOne very handy feature of Boolean operators is that they associate left to right, and theyshort-circuit, meaning that they do not even evaluate their second argument if their truthvalue is unambiguous from their first argument. For example, imagine that you wanted todetermine a very approximate ratio of two numbers, but also wanted to avoid a possible division-by-zero error. You can first test to make sure that the denominator is not zero byusing the !=(not-equal-to) operator: if ($denom != 0 && $numer / $denom > 2) print( More than twice as much! ); In the case where $denomis zero, the &&operator should return false regardless of whetherthe second expression is true or false. Because of short-circuiting, the second expression isnot evaluated, so an error is avoided. In the case where $denomis not zero, the &&operatordoes not have enough information to reach a conclusion about its truth value, so the secondexpression is evaluated. Cross- Reference08
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services