PHP-language, conditional statements Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management teppo.raisanen@oamk.fi, 050 382 6587 Control structures • Conditional structures – If – Switch • Loops – While – For • Functions • http://us.php.net/manual/en/language.controlstructures.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”; } else { if ($grade<5) { print ”B”; } else { if ($gade==5) { print ”A”; } else { 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 ifstatements with switch statement • Also logical and comparison operators are allowed Exercises • http://www.oamk.fi/~teraisan/K1053BI/exerci se.htm • Exercises 6 to 10 are about conditional statements