Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.

Similar presentations


Presentation on theme: "Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions."— Presentation transcript:

1 Lecture 2 Conditional Statement

2 chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions are performed for different decision. PHP has the following conditional statements If statement: executes a code only if a condition is met. If – else statement: executes a code if a condition is true and if false, executes another code. If – else if – else statement: if the first condition is not true, another condition is tested, if all condition fails, then a code is executed. Switch Statement: selects a block of code out of several blocks to be executed.

3 chcslonline.org IF Statement If statement executes a code only if a condition is met. It has the syntax: If (condition){ do something } For example <?php $image = “car”; If ($image == “car”){ print ‘ ’; } ?>

4 chcslonline.org So here we have used both the if statement and the comparison operator = = (equal). It checks if the value assigned to the variable $image is car, if it is, the image is displayed and if not, nothing is displayed. Assuming we change the value of $image to be moon. Nothing will be displayed because the condition is false.

5 chcslonline.org If-else statement Assuming we want to do something else if a condition is false, the if-else statement is used. It has the syntax If (condition){ do something } else{ Do something else } For example, lets try using another comparison operator.

6 chcslonline.org practical 5.2 Given x = 5, write a program that will check if the value of x is an odd number or an even number. To do this, we will first declare a variable x and then store the value 5 in it. Then we will write the if – else statement. We also know that every even number is divisible by 2 without remainder and every odd number will have a remainder when divided by 2. to test this condition, we will use the modulus operator to check if dividing the value of x by 2 gives a remainder or not then a comparison operator to check.

7 chcslonline.org solution <?php $x = 5; if ($x%2 !=0){ print “X is an odd number”; } else{ print “X is an even number”; }

8 chcslonline.org If-else if -else Assuming we have more than two conditions to test. The if – else if –else statement is used. It has the syntax if (condition){ Do something; } else if (another condition){ Do something; } else{ Do something else; }

9 chcslonline.org Switch statement Assuming we have more than two conditions to test, multiple if statement will be more complex to handle this. The best way will be to use a switch statement. It chooses one block of code out of several blocks to execute if the condition for that code evaluates to true. It has the syntax. Switch (n){ case x: Do something; Break; Case x: Do something; Break; Default: Do something; } Where n is the variable name and x is the value of the variable we are testing. The break is used to prevent the code from automatically running into the next block of code The default is used to handle a situation when all case fails or evaluates to false.

10 chcslonline.org practical 5.3 Write a program to output the class of a grade depending on the Grade of the user. <?php $grade =“A” ; Switch ($grade){ Case “A”: echo “Excellent”; break; Case “B”: Echo “very good”; Break; Case “C”: Echo “Average”; Break; Default: Echo “Please Enter a valid grade”; }


Download ppt "Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions."

Similar presentations


Ads by Google