php control statement

php control statement

php control statement

PHP offers several control statements to control the flow of program execution based on different conditions. Some of the commonly used control statements in PHP are:

  1. If statement: The if statement is used to execute a block of code only if a certain condition is true.

Example:

				
					<?php
    $x = 10;
    if ($x > 5) {
        echo "x is greater than 5";
    }
?> 

				
			

 

  1. If-else statement: The if-else statement is used to execute a block of code if a certain condition is true, and another block of code if the condition is false.

Example:

				
					phpCopy code
<?php $x = 10; if ($x > 5) { echo "x is greater than 5"; } else { echo "x is less than or equal to 5"; } ?> 

				
			
  1. Switch statement: The switch statement is used to select one of many blocks of code to be executed.

Example:

				
					phpCopy code
<?php $x = 2; switch ($x) { case 1: echo "x is equal to 1"; break; case 2: echo "x is equal to 2"; break; default: echo "x is not equal to 1 or 2"; } ?> 

				
			
  1. While loop: The while loop is used to execute a block of code as long as a certain condition is true.

Example:

				
					<?php
    $x = 1;
    while ($x <= 5) {
        echo "The value of x is: $x <br>";
        $x++;
    }
?>

				
			
  1. For loop: The for loop is used to execute a block of code a specified number of times.

Example:

				
					phpCopy code
<?php for ($x = 0; $x <= 5; $x++) { echo "The value of x is: $x <br>"; } ?> 

				
			

 

  1. Foreach loop: The foreach loop is used to iterate over each element in an array.

Example:

				
					<?php
    $colors = array("Red", "Green", "Blue");
    foreach ($colors as $color) {
        echo "The color is: $color <br>";
    }
?>

				
			
  1. do while loop: in PHP, the do-while loop is a control structure that is used to execute a block of code at least once, and then continue to execute the block of code as long as a certain condition is true.

      Example:

				
					<?php
    $x = 1;
    do {
        echo "The value of x is: $x <br>";
        $x++;
    } while ($x <= 5);
?>

				
			

Output:

				
					The value of x is: 1
The value of x is: 2
The value of x is: 3
The value of x is: 4
The value of x is: 5

				
			

 

break and continue statement in php:

In PHP, the break and continue statements are control statements that are used to control the flow of execution in loops.

  1. The break statement is used to immediately exit a loop. It is often used in conjunction with an if statement to check for a certain condition and exit the loop if the condition is met. Once the break statement is encountered, the loop is terminated and the program execution continues from the statement immediately after the loop.
				
					phpCopy code
<?php for ($i = 1; $i <= 10; $i++) { if ($i == 5) { break; } echo "The value of i is: $i <br>"; } ?> 

				
			

In this example, the loop will be terminated when $i is equal to 5, and the output will be:

				
					pythonCopy code
The value of i is: 1 The value of i is: 2 The value of i is: 3 The value of i is: 4 

				
			
  1. The continue statement is used to skip the rest of the loop and move on to the next iteration. It is often used in conjunction with an if statement to check for a certain condition and skip the current iteration of the loop if the condition is met.

Example:

				
					phpCopy code
<?php for ($i = 1; $i <= 10; $i++) { if ($i % 2 == 0) { continue; } echo "The value of i is: $i <br>"; } ?> 

				
			

In this example, the loop will skip over even numbers and continue with odd numbers, and the output will be:

				
					The value of i is: 1
The value of i is: 3
The value of i is: 5
The value of i is: 7
The value of i is: 9 

				
			

Note that both the break and continue statements can be used in any type of loop in PHP, including the for, while, and do-while loops.

Join To Get Our Newsletter
Spread the love