PHP-language, conditional statements Jouni Juntunen Oulu University of Applied Sciences School of Business and Information Management
Control structures Conditional structures Loops Functions If Switch Loops While For Functions http://us.php.net/manual/en/language.control-structures.php
Basic syntax if (expression) { ... } else
Comparison operators
Example <? //Read value passed from HTML-form. $age=$_POST[’age’]; //Found out if user is minor or adult according to age. if ($age<18) { print ”Minor”; } else print ”Adult”; ?>
Nested statements <? $grade=$_POST[’grade’]; If ($grade==0) { print ”F”; } else if ($grade<3) print ”C”; if ($grade<5) print ”B”; if ($gade==5) print ”A”; print ”Not on scale”; ?>
Logical operations || OR && AND ! NOT
Example <? $grade=$_POST[’grade’]; //If grade is not between 0 and 5 it is //not on scale. If ($grade<0 || $grade>5) { print ”Not on scale”; } ?>
Switch Sometimes you can replace multiple if-statements with switch statement Also logical and comparison operators are allowed
Exercises http://www.oamk.fi/~jjuntune/php/exercises.php Exercises 6 to 10 are about conditional statements