PHP break

What is Break in PHP?

Inside the loop, the break statement is used to control the loop in preprocessor hypertext. PHP utilizes the break keyword to implement the break statement, which transfers the program's flow at the end of the loop and skips the current statement once it is reached.

  • Break is a predefined keyword in PHP.
  • The break statement can’t be used outside of the iteration loop. It only appears in iteration statements and causes the control to be passed to the end of that loop in which it is used. To be more specific, each of the statements.
  • The break statement is used in all php control loops (for, while, and do-while).

Syntax:

The syntax of the continue statement is: 
     Jump statement;
     break;

The statements within the loop are executed based on the loop condition. If the specified condition is true, the loop iteration will be broken and the loop is terminated. The break statement skips all the statements and passes control to the statement after the loop.

Syntax of for loop:

for (initialize; condition; increment) 
 {
 statement(x);
 }

Use of Break statement in for loop:

The break keyword in a for loop skips all the remaining statements if the specified condition is true.

  for ( , , ) {         
   {                                           
     // ...            
  break;
   }
 } 

Syntax of the while loop:

the syntax for a while loop is given by

while(condition)
                     {
                       
statement(x);
                     }

Use of Break statement in while loop:

  while () {         
 {                                           
     // ...            
 }                                           
 break;
 } 

Syntax of Do-While loop:

        do{ 
              statement(s);
             } 
         while(expression);

Use of Break statement in Do-While loop

do ( ){      
   {               
     // ...          
  break;
updation;
   }
 } while(,) 

Let us take some examples to understand the continue statement in PHP:

Program 1: Program to print numbers:

<?php  
    //outer loop 
    for($i =1; $i<=3; $i++)
               {  
        //inner loop
        for($j=9; $j<=1; $j--) 
                  {  
                     if(($i == $j) )
                           {  
                               break;           // it breaks if both i and j are equal
                           }  
            echo $i.$j;  
             echo “</br>”;
        }  
    }  
             ?>  

The output of the program:

19         
18
17
16
15
14
13
12

Program 2: Program to print number between 1 and 10 by using the Break statement on 11th number:

<?php  
      echo " numbers from 1 to 10: </br>";  
    $i = 1;  
    while($i<=20){
   if($i==11){ 
            $i++; 
       break;     //it skips all even numbers
        }  
        echo $i;  
        echo”</br>”; 
        $i++;  
               }     
?>  

The output of the program:

1
2
3
4
5
6 
7
8
9
10

Explanation:

The program is designed to print the first ten natural numbers. We have used the while loop in the above code to demonstrate the implementation of the break statement.

We've set up two variables: "n" for entering the value and "i" for the iteration. The program asks the user for a number, which is then stored in variable ‘n’. We initialized i to 1(i=1) and used a condition as text expression that verifies the value of "i" is less than or equal to 20 (i<=20). We used the break statement inside the loop to skip the numbers more than 10. If the condition is true according to the given logic then the loop is brokened.  If the number is less than 10 , the statement outside if will execute, and the numbers are printed one by one.

Program 3: Program to print the numbers:

<?php
for ($i = 1; $i<=5; $i++)
 {
   echo "outer loop\n";
   for ($j=1;$j<=5;$j++)
 {
      if ($j >5) 
 break2;
      echo "I : $i J : $j"."\n";
   }
   echo "End of inner loop\n";
}
?>

The Output of the program:

outer loop
i : 1 J : 1
i : 1 J : 2
i : 1 J : 3
i: 1 j: 4
outer loop
i : 2 J : 1
i : 2 J : 2
i : 2 J : 3
i:2  j : 4
outer loop
i : 3 J : 1
i : 3 J : 2
i : 3 J : 3
I : 3 j : 4
outer loop
i : 4 J : 1
i : 4 J : 2
i : 4 J : 3
i: 4 j : 4
outer loop
i : 5 J : 1
i : 5 J : 2
I : 5 J : 3
I: 5 j :4

Related Topics

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP array_rand() Function

PHP array_rand() Function The array_rand() function in PHP picks one or more random keys out of an array. Syntax array_rand ( array $array [, int $num = 1 ] ) Parameter array(required)- This parameter represents the input array. num(optional)- This parameter specifies how many entries should be picked....

1 minute read.

PHP is_object() Function

The is_ object() function in PHP finds whether the specified variable is an object or not. It returns a Boolean value TRUE if the specified var is an object else it returns FALSE. Syntax is_object(mixed$var) var(required)- This parameter represents the...

1 minute read.

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP Example

We can create a simple program by writing the source code inside the php tag and save it with .php extension. Syntax: <?php // your code here ?> Let us take an example of PHP. <!DOCTYPE html> <html> <head>            ...

1 minute read.

PHP doubleval() Function

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.

PHP vfprintf() Function

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP unset() Function

PHP unset() Function The unset() function in PHP is used to unset a given variable. This function destroys the specified variables. If this function is called inside any user-defined function, then it unsets the value associated with...

1 minute read.

PHP strrrev() Function

PHP strrrev () Function The strrev() function in PHP reverses a string. Syntax strrev ( string $string ) Parameter string(required)- This parameter represents the input string to be reversed. Return This function returns the reversed string. Example 1 Output Initial string: Hello...

1 minute read.

PHP array_udiff() Function

PHP array_udiff() Function The array_udiff() function in PHP calculates the difference of arrays by using a callback function for data comparison. Syntax array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func ) Parameter array1(required)- This array represents the first input array. array2(required)- This array represents the second input array. array3….(optional)- It...

1 minute read.