When creating a program to make decisions, we must first create a list of possible outcomes. For example, we could be asking the computer if a number is 65. The possible answers we give the computer could be as simple as yes or no, or we could add more options such as higher, equal, lower or even different ranges as to how far off we were from 65 with our guess.
For the sake of example let's give the computer three options for response. It can respond by telling us that the number is higher than 65, that the number is lower than 65, or that the number is in fact 65.
Now how does the computer go about determining which answer to give us? This is where boolean logic comes in. We must ask the computer a series of yes or no questions. For example, if we ask the computer if the number is greater than 65, and it decides yes then we do not need to ask if it is equal or below 65. We can simply answer the question.
If the computer decides to tell us that the number is not greater than 65, we then ask our next question, is the number equal to 65? If the answer is yes, we can tell this to the user. If the answer is no, we can tell the user that the number is lower than 65, however if you prefer you can make one last check by directly asking the computer if the number is lower than 65.
The first way we are going to look at how to pose these questions to the computer is by using IF. Here is how we compare our number (in this case $x) to the number 65:
<?php
if ($x > 65)
{
echo "x is larger than 65";
}
elseif ($x == 65)
{
echo "x is equal to 65";
}
else
{
echo "x is smaller than 65";
}
?> Basically what this code is doing is first checking to see if x is greater than 65. If that is true, it executes the code within the brackets, in this case ECHOing that x is larger than 65. Then it looks at the ELSEIF. If x is equal to 65, it will execute the code within those brackets, in this case telling us that x is equal to 65. Finally, if neither of the following are true, it runs the ELSE code, and tells us that x is smaller than 65.
If there are only two possible outcomes we would removes the ELSEIF line, and keep the IF and the ELSE parts. Or you can simply have an IF line the does something when true, and have no ELSE if you want nothing to happen if the code is not true.
Next, I am going to show you how to make the same decision using a SWITCH loop. Here is an example of that:
<?php
switch ($x)
{
case $x > 65:
echo "x is larger than 65";
break;
case $x == 65:
echo "x is equal to 65";
break;
default:
echo "x is smaller than 65";
}
?> This works much the same way that the earlier code did. There are three options, either x can be greater than 65, x can be equal to 65, or it can return the default response that x is smaller than 65. In a SWITCH loop, instead of nesting IF and ELSEIF statements, all of the possibilities are listed as CASEs. In the example above we use two CASEs, and one default response to be used if neither of the CASEs are true.
Switch loops are great when you have lots of different possible answers, so that you don't have to keep nesting ELSEIF. For example, even something as simple as the day of the week has seven possible outcomes. Placing them in a switch loop makes a lot of sense.

