0% found this document useful (0 votes)
50 views

Flow Control Structures

- PHP provides various control structures like if, if-else, if-elseif, switch to conditionally execute code based on boolean expressions or variable values. - These control structures allow executing different code blocks based on whether conditions are true or false. For example, if-else executes one code block if the condition is true and another if it's false. - Nested control structures can be used to check multiple conditions, like an if statement within another if statement.

Uploaded by

moses maned
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Flow Control Structures

- PHP provides various control structures like if, if-else, if-elseif, switch to conditionally execute code based on boolean expressions or variable values. - These control structures allow executing different code blocks based on whether conditions are true or false. For example, if-else executes one code block if the condition is true and another if it's false. - Nested control structures can be used to check multiple conditions, like an if statement within another if statement.

Uploaded by

moses maned
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Flow Control Structures

Any PHP script is built out of a series of statements. A statement can be an


assignment, a function call, a loop, a conditional statement or even a statement
that does nothing (an empty statement).
Statements usually end with a semicolon. In addition, statements can be
grouped into a statement-group by encapsulating a group of statements with
curly braces. A statement-group is a statement by itself as well.
Sequential Logic (Sequential Flow) Sequential logic as the name suggests
follows a serial or sequential flow in which the flow depends on the series of
instructions given to the computer. Unless new instructions are given, the
modules are executed in the obvious sequence. The sequences may be given,
by means of numbered steps explicitly. Also, implicitly follows the order in
which modules are written. Most of the processing, even some complex
problems, will generally follow this elementary flow pattern.
Selection control structure Situations arise whereby a program need to
carry out a logical test, and then take an alternative action depending
on the outcome of Boolean test. A Boolean test is an expression that
uses relational operators; like = (equal to), > (greater than), < (less
than), >= (greater than or equal to) and <= (less than or equal to) and
three logical operators namely AND, OR and NOT. In this section we will
see
• if
• if-else
• if-else-if
• nested if
• switch
PHP If Statement

• PHP if statement allows conditional execution of code. It is executed if


condition is true.
• If statement is used to executes the block of code exist inside the if
statement only if the specified condition is true.
Syntax
if(condition){  
//code to be executed  
}  
Example
<?php  
$num=12;  
if($num<100){  
echo "$num is less than 100";  
}  
?>  
PHP If-else Statement

• PHP if-else statement is executed whether condition is true or false.


• If-else statement is slightly different from if statement. It executes one
block of code if the specified condition is true and another block of
code if the condition is false.
Syntax
if(condition){  
//code to be executed if true  
}else{  
//code to be executed if false  
}  
Example
<?php  
$num=12;  
if($num%2==0){  
echo "$num is even number";  
}else{  
echo "$num is odd number";  
}  
?>  
PHP If-else-if Statement

• The PHP if-else-if is a special statement used to combine multiple if?.else statements. So,
we can check multiple conditions using this statement.
Syntax
if (condition1){    
//code to be executed if condition1 is true    
} elseif (condition2){      
//code to be executed if condition2 is true    
} elseif (condition3){      
//code to be executed if condition3 is true    

}  else{    
//code to be executed if all given conditions are false    
}    
Example
<?php
$marks=69;
if ($marks<33){
echo "fail";
}
else if ($marks>=34 && $marks<50) {
echo "D grade";
}
else if ($marks>=50 && $marks<65) {
echo "C grade";
}
else if ($marks>=65 && $marks<80) {
echo "B grade";
}
else if ($marks>=80 && $marks<90) {
echo "A grade";
}
else if ($marks>=90 && $marks<100) {
echo "A+ grade";
}
else {
echo "Invalid input";
}
?>
PHP nested if Statement

• The nested if statement contains the if block inside another if block.


The inner if statement executes only when specified condition in outer
if statement is true.
Syntax
if (condition) {    
//code to be executed if condition is true   
if (condition) {    
//code to be executed if condition is true    
}    
}   
Example
<?php  
$age = 23;  
$nationality = "Indian";  
//applying conditions on nationality and age  
if ($nationality == "Indian")  
 {  
if ($age >= 18) {  
 echo "Eligible to give vote";  
}  
else {    
echo "Not eligible to give vote";  }  
}  
?>  
<?php  
 $a = 34; $b = 56; $c = 45;  
if ($a < $b) {  
if ($a < $c) {  
echo "$a is smaller than $b and $c";   }  
}  
?>  
PHP Switch
• PHP switch statement is used to execute one statement from multiple conditions. It works like
PHP if-else-if statement.
Syntax
switch(expression){      
case value1:      
//code to be executed  
break;  
case value2:      
//code to be executed  
break;  
.....      
default:       
code to be executed if all cases are not matched;    
}  
Important points to be noticed about switch case:

1.The default is an optional statement. Even it is not important, that default must


always be the last statement.
2.There can be only one default in a switch statement. More than one default may
lead to a Fatal error.
3.Each case can have a break statement, which is used to terminate the sequence of
statement.
4.The break statement is optional to use in switch. If break is not used, all the
statements will execute after finding matched case value.
5.PHP allows you to use number, character, string, as well as functions in switch
expression.
6.Nesting of switch statements is allowed, but it makes the program more complex
and less readable.
7.You can use semicolon (;) instead of colon (:). It will not generate any error.
PHP Switch Example
<?php      
$num=20;      
switch($num){      
case 10:      
echo("number is equals to 10");      
break;      
case 20:      
echo("number is equal to 20");      
break;      
case 30:      
echo("number is equal to 30");      
break;      
default:      
echo("number is not equal to 10, 20 or 30");      
}     
?>  
PHP switch statement with character

• Program to check Vowel and consonant


• We will pass a character in switch expression to check whether it is
vowel or constant. If the passed character is A, E, I, O, or U, it will be
vowel otherwise consonant.
<?php      
$ch = 'U';  
 switch ($ch)  
 {     
case 'a':   
echo "Given character is vowel";  
  break;  
 case 'e':   
 echo "Given character is vowel";  
 break;  
case 'i':   
echo "Given character is vowel";  
 break;  
case 'o':   
 echo "Given character is vowel";  
 break;    
 case 'u':   
 echo "Given character is vowel";  
break;  
 case 'A':   
echo "Given character is vowel";  
 break;  
case 'E':   
 echo "Given character is vowel";  
   break;  
case 'I':   
   echo "Given character is vowel";  
 break;  
case 'O':   
echo "Given character is vowel";  
 break;  
case 'U':   
 echo "Given character is vowel";  
break;  
default:   
echo "Given character is consonant";  
   break;  
}  
?>    

You might also like