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 sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP is_string() Function

PHP is_string() Function The is_ string() function in PHP finds whether a variable is a string or not. It returns a Boolean value TRUE if the parameter var is string, else it returns FALSE. Syntax is_string ( mixed $var ) Parameter var(required)- This parameter represents...

1 minute read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP strripos() Function

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP convert_uudecode() Function

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

1 minute read.

PHP print_r() Function

PHP print_r() Function The print_r() function in PHP displays the information stored in a variable. Syntax print_r (mixed $expression [, bool $return] ) Parameter expression(required)- This parameter represents the expression to be printed. return(optional)- This parameter is used to store the output of...

2 minutes read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

1 minute read.

PHP Echo And Print

In PHP, echo and print is used to get output data to the screen but there is some difference, between echo and print. PHP echo PHP echo is used to display the...

1 minute read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.

PHP substr() Function

PHP substr() Function The substr() function in PHP returns the portion of the string specified by the start and length parameters. Syntax substr ( string $string , int $start [, int $length ] )  Parameter string (required)- This parameter represents the input string. It must be one character or longer. start (required)- This parameter specifies where to start...

2 minutes read.

PHP nl2br() Function

PHP nl2br() Function The nl2br() function in PHP inserts HTML line breaks before all newlines in a string. Syntax nl2br ( string $string [, bool $is_xhtml] ) Parameter sting(required)- This parameter specifies the input string. is_xhtml(optional)- This parameter states whether to use XHTML compatible line breaks...

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 Date and Time

DATE: The PHP date() function formats a date or time. The timestamp is used to format a more readable date and time into a PHP date () function. Syntax: date(format,timestamp) Simple Date: Characters that...

1 minute read.

PHP uksort() Function

PHP uksort() Function The uksort() function in PHP sorts an array by keys using a user-defined comparison function. Syntax uksort ( array &$array , callable $key_compare_func ) Parameter array(required)- This parameter represents the input array. key_compare_func(required)- This parameter represents a string that describe a callable comparison function....

1 minute read.

PHP strtolower() Function

The strtolower() function in PHP is used to convert a string to lowercase. This function is binary-safe. The related functions are as follows: strtoupper()lcfirst()ucfirst()ucwords() Syntax strtolower ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP Operator

PHP Operator is used to perform operations on variable and values. Following are the types of Operator: PHP Arithmetic Operator PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication...

2 minutes read.

PHP array_multisort() Function

PHP array_multisort () Function The array_multisort() function in PHP sorts multiple or multi-dimensional arrays at once. Syntax array_multisort ( array &$array1 [, mixed $array1_sort_order [, mixed $array1_sort_flags [, mixed $... ]]] ) Parameter array1(required)- This parameter specified the input array to sort. array1_sort_order(optional)- This parameter specifies the order (ascending or descending)...

1 minute read.

PHP prev() Function

PHP prev() Function The prev() function in PHP rewinds or moves back the internal array pointer one place backward and returns the prev array value. It behaves like next() function, except that this function rewinds the...

1 minute read.