- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
break and continue Statement in PHP
This article is created to cover the two important statements or keywords in PHP, that are:
- break
- continue
PHP break Statement
The PHP break keyword or statement, is used when we need to exit from the current loop or switch structure. For example:
<?php for($i=1; $i<10; $i++) { if($i>5) break; echo $i; echo "<BR>"; } ?>
The snapshot given below shows the output produced by this PHP example on break statement:
In above example, when the value of $i becomes greater than 5, that is when the value of $i becomes 6, then the condition $i>5 or 6>5 evaluates to be true, therefore, the program flow goes inside the if block and the break; statement gets executed, that skips the remaining execution of the current for loop. Therefore, we only get the output from 1 to 5.
The break statement skips the execution of remaining statement(s) (if any) along with all the remaining iterations of the current loop.
The break statement can also be used to stop the execution of an infinite loop (the loop whose condition always evaluates to be true). For example:
<?php $num = 10; while(true) { echo $num, "<BR>"; if($num==12) break; $num++; echo $num, "<BR>"; } ?>
The output of this PHP example should be:
See the condition of the loop, it is true, that obviously evaluates to be true for ever. Therefore, using the break statement, the execution of this loop ends.
The dry run of above example goes like:
- The value 10 initialized to the variable $num using the first statement
- Now the execution of the while loop started
- Since the condition of the loop is true, therefore, the loop goes for its infinite execution
- At first iteration, using the echo, the value of $num gets printed along with a line break using BR tag
- Since the condition $num==12 or 10==12 evaluates to be false, therefore program flow will not go inside the if block to execute the break statement
- The value of $num gets incremented by 1. Now $num=11
- The value of $num, again gets printed along with a line break
- Now the second iteration of the loop starts
- This process continues, until the value of $num becomes 12
- When the value of $num becomes 12, then the condition $num==12 (of if) evaluates to be true, and the break; statement gets executed, that forces the while loop to stop its execution
PHP continue Statement
The PHP continue statement or keyword is used, when we need to skip the execution of remaining statement(s) for the current iteration of the current loop, and continue for the next iteration of the current loop. For example:
<?php for($i=1; $i<10; $i++) { if($i==5) continue; echo $i; echo "<BR>"; } ?>
The output of above PHP example on continue statement, is:
In above example, when the value of $i becomes 5, then the condition $i==5 evaluates to be true. Therefore the continue; statement gets executed, that forces to jump and continue the execution of next iteration of the current loop. That is, the two echo statement gets skipped for that iteration.
Difference Between break and continue in PHP
The break is used to skip all the remaining execution and iteration of the current loop. Whereas the continue is used to skip the execution of the remaining statement(s) of the current iteration of the current loop. For example:
<?php echo "<h2>Using the break Statement</h2>"; for($i=1; $i<=5; $i++) { if($i==3) break; echo $i, "<BR>"; } echo "<h2>Using the continue Statement</h2>"; for($i=1; $i<=5; $i++) { if($i==3) continue; echo $i, "<BR>"; } ?>
The output of above PHP example, is:
« Previous Tutorial Next Tutorial »