Visit the Official PHP Website |
PHP ProgrammingBy James N HitzThe if... elseif... Decision ConstructThe if... elseif... structure is a bigger sister of the if and if...else because it is more versatile and can thus be used for the testing of multiple conditions. The word elseif is just that... elseif. Don't be tempted to put a space between else and if. An else can also be added to the if... elsif.. to supply a "default" solution. The syntax is: if(condition 1){ statement block 1 statement block 1 } elseif(condition 2) { statement block 2 statement block 2 } elseif(condition 3) { statement block 3 statement block 3 } else { statement block 4 statement block 4 } That's a lot of code! Phew! lets see how that would look like dillute... if($age <=8){ print "Too young. How are you reading this?!"; } elseif( ($age>9) && ($age<=17) ){ print "You are a teenager"; } elseif( ($age>=18)&&($age<=90)){ print "Adult... Right?"; } else { print "Gotcha Granpa' You should take a rest"; } This code simppest makes 3 tests:
But there is a statement block that is added to cater for any other situation. The only other situation that can exist in this case is where the age is greater than or equal to 90. For these senior citizens it is a "Gotcha Granpa' You should take a rest" message. Well. This can be... really useful wouldn't you say? Especially if you are trying to get granpa' to stop being a web-o-holic like you. << The if... else Control Structure | The switch() Decision Construct >> |
JamHitz Productions