HOW TO WRITE (Yahoo web space) PHP SCRIPTS so testing stops
Friday, November 9th, 2007HOW TO WRITE PHP SCRIPTS so testing stops as soon as one turns out to be false. Similarly, when using ||, only one condition needs to be fulfilled, so testing stops as soon as one turns out to be true. $a = 10; $b = 25; if ($a > 5 && $b > 20) // returns true if ($a > 5 || $b > 30) // returns true, $b never tested The implication of this is that when you need all conditions to be met, you should design your tests with the condition most likely to return false as the first to be evaluated. When you need just one condition to be fulfilled, place the one most likely to return true first. If you want a particular set of conditions considered as a group, enclose them in parentheses. if (($a > 5 && $a < 8) || ($b > 20 && $b < 40)) PHP also uses AND in place of && and OR in place of ||. However, they aren t exact equiv- alents. To avoid problems, it s advisable to stick with && and ||. Using the switch statement for decision chains The switch statement offers an alternative to if... else for decision making. The basic structure looks like this: switch(variable being tested) { case value1: statements to be executed break; case value2: statements to be executed break; default: statements to be executed } The case keyword indicates possible matching values for the variable passed to switch(). When a match is made, every subsequent line of code is executed until the break keyword is encountered, at which point the switch statement comes to an end. A simple example follows: switch($myVar) { case 1: echo '$myVar is 1'; break; case 'apple': echo '$myVar is apple'; break; default: echo '$myVar is neither 1 nor apple'; }
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.