- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
Difference between exit and break in PHP
This article is created to differentiate between exit() and break in PHP.
The PHP exit() method stops the current PHP script from running. The break statement, on the other hand, stops the current loop or switch structure from running. For example, let me use break first:
<?php for($i=1; $i<10; $i++) { if($i==4) break; echo 'The value of $i is ' . $i . '<br>'; } echo '<hr>'; echo 'Some text right after the "for" loop...<br>'; echo 'Some other text...<br>'; ?>
The following line of code initializes a variable $i to 1, and sets the loop condition to continue while $i is less than 10. On each iteration, the variable $i is incremented using the post-increment operator $i++.
for($i=1; $i<10; $i++):
And the following block of code
if($i==4) break;
checks if the value of $i is equal to 4, and if so, it breaks out of the loop using the break statement. In this case, since the loop condition is set to continue while $i is less than 4, this if statement is unnecessary and will never be executed.
The next line of code prints the value of "$i" to the screen, along with some extra text. The dot operator (.) concatenates the string "The value of $i is" with the value of "$i" and the "<br>" tag, which adds a line break.
echo 'The value of $i is ' . $i . '<br>';
The output of this PHP example is:
And let me create the same example, using exit():
<?php for($i=1; $i<10; $i++) { if($i==4) exit(); echo 'The value of $i is ' . $i . '<br>'; } echo '<hr>'; echo 'Some text right after the "for" loop...<br>'; echo 'Some other text...<br>'; ?>
Similar to "<br>", the "<hr>" tag creates a horizontal line. Now the rest of the code is easily understandable based on the previous explanation.
Now in this example, since I used "exit()" (which stops the complete PHP scripts from execution) instead of "break" (which only breaks or stops the current loop's execution), the remaining PHP script or code will not be executed, even if the code is written outside the loop. Therefore, the output should be:
Another thing I wanted to mention is that both exit() and exit can be used to end a script's execution. However, because exit() is a function and follows the standard function call syntax in PHP, it is considered to be the more "proper" way of doing so.
That is, the break is used when we need to exit from a loop, whereas the exit is used when we need to exit from the whole PHP script or program.
« Previous Tutorial Next Tutorial »