87Chapter 6Control and (Professional web hosting) FunctionsWatch out for a very
87Chapter 6Control and FunctionsWatch out for a very common mistake: confusing the assignment operator (=) with the com- parison operator (==). The statement if($three=$four).will (probably unexpectedly) set the variable $threeto be the same as $four; what s more, the test will be true if $fouris a true value! Operator precedenceAlthough overreliance on precedence rules can be confusing for the person who reads yourcode next, it s useful to note that comparison operators have higher precedence than Booleanoperators. This means that a test like the following: if ($small_num > 2 && $small_num < 5) ... doesn t need any parentheses other than those shown. String comparisonThe comparison operators may be used to compare strings as well as numbers (see the cautionary sidebar). We would expect the following code to print its associated sentence(with apologies to Billy Bragg): if (( Marx < Mary ) and( Mary < Marzipan )) { print( Between Marx and Marzipan in the ); print( dictionary, there was Mary.
); } The comparisons are case sensitive, and the only reason that this example will print anythingis because our values are case-consistent.Because of the capitalization of Dennis, the follow- ing will not print anything: if (( deep blue sea < Dennis ) and( Dennis < devil )) { print( Between the deep blue sea and ); print( the devil, that was me.
); } The ternary operatorOne especially useful construct is the ternary conditional operator, which plays a role some- where between a Boolean operator and a true branching construct. Its job is to take threeexpressions and use the truth value of the first expression to decide which of the other twoexpressions to evaluate and return. The syntax looks like: test-expression ? yes-expression : no-expressionThe value of this expression is the result of yes-expressionif test-expressionis true; otherwise, it is the same as no-expression. For example, the following expression assigns to $max_numeither $first_numor$second_num, whichever is larger: $max_num = $first_num > $second_num ? $first_num : $second_num; Caution08
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services