PHP Conditional Statement.pptx
Document Details
Uploaded by TrustingPeridot
Full Transcript
PHP CONDITIONAL STATEMENTS Learning Objectives • To understand the importance of conditional statement • To know different type of conditional statements in PHP • To know the basic fundamental logic creation using conditional statement • To apply the PHP syntax using conditional statements in crea...
PHP CONDITIONAL STATEMENTS Learning Objectives • To understand the importance of conditional statement • To know different type of conditional statements in PHP • To know the basic fundamental logic creation using conditional statement • To apply the PHP syntax using conditional statements in creating a website. PHP conditional statement • Conditional statements are useful for writing decision making logic. • It is most important feature of many programming languages , including PHP. They are implemented by the following types: • if statement • if…else statement • if…elseif….else statement • switch statement If statement in PHP The if statement executes some code if one condition is true. SYNTAX: if( condition ) { Execute statement(s) if condition is true; } If else statement in PHP The if...else statement executes some code if a condition is true and another code if that condition is false. SYNTAX: If (condition) { Execute statement (s) if condition is true; } Else{ Execute statement(s) if condition is false; } If elseif else statement in PHP •The if...elseif...else statement executes different codes for more than two conditions. •If - elseif- else statement is a combination of if-else statement. SYNTAX If( 1st condition) { Execute statement(s )if condition is true; } { elseif( 2nd condition )Execute statement(s) if 2nd condition is true; }Else{ Execute statement(s) if both conditions are false; } Switch case: The switch statement is used to perform different actions based on different conditions. Use the switch statement to select one of many blocks of code to be executed. SYNTAX: switch (n) { case label 1: code to be executed if n=label 1; break; case label 2: code to be executed if n=label 2; case label 3: code to be executed if n=label 3; … default: code to be executed if n is different from all labels; } POINTS TO REMEMBER Conditional statements are used to perform different actions based on different conditions. Activity #5 write a program to check student grade based on marks whether A+, A, B, C, D grade or fail in PHP. Example: User input: 76 Output : D grade NEXT TOPIC: LOOPING STRUCTURE PHP LOOPS