Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, 3 January 2017

PHP Control Statements 1

PHP While Loop

PHP while loop can be used to traverse set of code like for loop.
It should be used if number of iteration is not known.
Syntax
1.            while(condition){  
2.            //code to be executed  
3.            }  
Alternative Syntax
1.            while(condition):  
2.            //code to be executed  
3.              
4.            endwhile;  
PHP While Loop Flowchart


PHP While Loop Example
1.            <?php  
2.            $n=1;  
3.            while($n<=10){  
4.            echo "$n<br/>";  
5.            $n++;  
6.            }  
7.            ?>  
Output:
1
2
3
4
5
6
7
8
9
10
Alternative Example
1.            <?php  
2.            $n=1;  
3.            while($n<=10):  
4.            echo "$n<br/>";  
5.            $n++;  
6.            endwhile;  
7.            ?>  
Output:
1
2
3
4
5
6
7
8
9
10

PHP Nested While Loop

We can use while loop inside another while loop in PHP, it is known as nested while loop.
In case of inner or nested while loop, nested while loop is executed fully for one outer while loop. If outer while loop is to be executed for 3 times and nested while loop for 3 times, nested while loop will be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop).
Example
1.            <?php  
2.            $i=1;  
3.            while($i<=3){  
4.            $j=1;  
5.            while($j<=3){  
6.            echo "$i   $j<br/>";  
7.            $j++;  
8.            }  
9.            $i++;  
10.         }  
11.         ?>  
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

PHP do while loop

PHP do while loop can be used to traverse set of code like php while loop. The PHP do-while loop is guaranteed to run at least once.
It executes the code at least one time always because condition is checked after executing the code.
Syntax
1.            do{  
2.            //code to be executed  
3.            }while(condition);  
Flowchart

Example
1.            <?php  
2.            $n=1;  
3.            do{  
4.            echo "$n<br/>";  
5.            $n++;  
6.            }while($n<=10);  
7.            ?>  
Output:
1
2
3
4
5
6
7
8
9
10

PHP Break

PHP break statement breaks the execution of current for, while, do-while, switch and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only.
Syntax
1.            jump statement;  
2.            break;  
Flowchart

PHP Break: inside loop

Let's see a simple example to break the execution of for loop if value of i is equal to 5.
1.            <?php  
2.            for($i=1;$i<=10;$i++){  
3.            echo "$i <br/>";  
4.            if($i==5){  
5.            break;  
6.            }  
7.            }  
8.            ?>  
Output:
1
2
3
4
5

PHP Break: inside inner loop

The PHP break statement breaks the execution of inner loop only.
1.            <?php  
2.            for($i=1;$i<=3;$i++){  
3.             for($j=1;$j<=3;$j++){  
4.              echo "$i   $j<br/>";  
5.              if($i==2 && $j==2){  
6.               break;  
7.              }  
8.             }  
9.            }  
10.         ?>  
Output:
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3

PHP Break: inside switch statement

The PHP break statement breaks the flow of switch case also.
1.            <?php      
2.            $num=200;      
3.            switch($num){      
4.            case 100:      
5.            echo("number is equals to 100");      
6.            break;      
7.            case 200:      
8.            echo("number is equal to 200");      
9.            break;      
10.         case 50:      
11.         echo("number is equal to 300");      
12.         break;      
13.         default:      
14.         echo("number is not equal to 100, 200 or 500");      
15.         }     
16.         ?>      
Output:

number is equal to 200


PHP Control Statements


PHP If Else

PHP if else statement is used to test condition. There are various ways to use if statement in PHP.
  • if
  • if-else
  • if-else-if
  • nested if

PHP If Statement

PHP if statement is executed if condition is true.

Syntax
1.            if(condition){  
2.            //code to be executed  
3.            }  

Flowchart

Example
1.            <?php  
2.            $num=12;  
3.            if($num<100){  
4.            echo "$num is less than 100";  
5.            }  
6.            ?>  

Output:

12 is less than 100

PHP If-else Statement

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

Syntax

1.            if(condition){  
2.            //code to be executed if true  
3.            }else{  
4.            //code to be executed if false  
5.            }  

Flowchart

Example

1.            <?php  
2.            $num=12;  
3.            if($num%2==0){  
4.            echo "$num is even number";  
5.            }else{  
6.            echo "$num is odd number";  
7.            }  
8.            ?>  
Output:
12 is even number

PHP Switch

PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.

Syntax

1.            switch(expression){      
2.            case value1:      
3.             //code to be executed  
4.             break;  
5.            case value2:      
6.             //code to be executed  
7.             break;  
8.            ......      
9.            default:       
10.          code to be executed if all cases are not matched;    
11.         }  

PHP Switch Flowchart


PHP Switch Example

1.            <?php    
2.            $num=20;    
3.            switch($num){    
4.            case 10:    
5.            echo("number is equals to 10");    
6.            break;    
7.            case 20:    
8.            echo("number is equal to 20");    
9.            break;    
10.         case 30:    
11.         echo("number is equal to 30");    
12.         break;    
13.         default:    
14.         echo("number is not equal to 10, 20 or 30");    
15.         }   
16.         ?>    

Output:

number is equal to 20

PHP For Loop

PHP for loop can be used to traverse set of code for the specified number of times.
It should be used if number of iteration is known otherwise use while loop.

Syntax

1.    for(initialization; condition; increment/decrement){  
2.    //code to be executed  
3.    }  

Flowchart
Example

1.            <?php  
2.            for($n=1;$n<=10;$n++){  
3.            echo "$n<br/>";  
4.            }  
5.            ?> 

Output:
1
2
3
4
5
6
7
8
9
10

PHP Nested For Loop

We can use for loop inside for loop in PHP, it is known as nested for loop.
In case of inner or nested for loop, nested for loop is executed fully for one outer for loop. If outer for loop is to be executed for 3 times and inner for loop for 3 times, inner for loop will be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop).

Example

1.            <?php  
2.            for($i=1;$i<=3;$i++){  
3.            for($j=1;$j<=3;$j++){  
4.            echo "$i   $j<br/>";  
5.            }  
6.            }  
7.            ?>  

Output:

1 1
1 2
1 3
2 1
2 2
2 3

3 1
3 2
3 3

PHP For Each Loop

PHP for each loop is used to traverse array elements.

Syntax

1.    foreach$array as $var ){  
2.     //code to be executed  
3.    }  
4.    ?>  

Example
1.    <?php  
2.    $season=array("summer","winter","spring","autumn");  
3.    foreach$season as $arr ){  
4.      echo "Season is: $arr<br />";  
5.    }  
6.    ?>  
Output:
Season is: summer
Season is: winter
Season is: spring

Season is: autumn