Visit the Official PHP Website |
PHP ProgrammingBy James N HitzThe if... else Decision ConstructThe previous code only instructs on what to do "if the $age is 18" but does not tell us what to do "when it is not equal to 18". This can be solved by adding an else to the if in a construct called and if... else The syntax for the if... else is: if(test expression) { statement 1 statement 2 statement 3... } else { statement 1 statement 2... } Notice here we have 2 statement blocks. One after if(...) the other after else. Let's put it in a more 'English' manner using our age example: if($age==18){ print "you are a young adult"; } else { Print "I don't know how old you are"; } If the age supplied is equal to 18, then the program will behave like it did before by printing the line "you are a young adult" If however the age supplied is different, the program will not just "keep quite", it will say " I don't know how old you are" So we will use the first method to only test ONE condition and discard the rest, and the second if we want to test ONE condition and yet provide a way out for the rest. What about if we have more than one conditions to test? elseif to the rescue. << The if Control Structure | The if... elseif... Decision Construct >> |
JamHitz Productions