- 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 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 and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- 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 Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP exit() | Stop Execution of Current File
The PHP exit() function is used, when we need to terminate the execution of current PHP script/file. For example:
<?php if(isset($_SESSION['user'])) { // block of code to process user } else { header('Location: login.htm'); exit(); // some block of code } // other block of code ?>
Since the session variable user is not set, therefore the program flow goes to else block and the following two statements gets executed:
header('Location: login.htm'); exit();
The header() redirect the page to the login.htm page, whereas the exit() function skips the execution of remaining PHP script. That is, the execution of current PHP scripts stops, when an exit() function encounters.
The output of above PHP example, is shown in the snapshot given below:
Since the header() function redirects to other page, that is login.htm page. Therefore, I think, I have to create another example of exit() function, that shows, how it terminates the execution of current PHP script:
<?php $x = 10; for($i=1; $i<$x; $i++) { if($i==4) exit(); echo "The value of \$i is $i"; echo "<BR>"; } echo "The value of \$i is $i"; echo "<BR>"; echo "Some other text..."; // some other block of code... ?>
The output of above PHP example, is shown in the snapshot given below:
That is, when the value of $i becomes equal to 4, then the exit() function encounters, that terminates the execution of the current PHP scripts. Therefore, in above example, including remaining values of $i, the statements available right after the loop, also has not been executed.
PHP exit() Syntax
The syntax of exit() function in PHP, is:
exit(message|status);
That is, either we can display some message, before stopping the executing of current PHP script, or can provide some status code. The status code/number will not displayed on the output, rather it is used for exit status.
But let me tell you one thing that, when an exit(); statement encounters, then the execution of current PHP scripts halts/stops, but shutdown functions and object destructors will complete its execution.
Note - If you are not passing any status code or message to the exit() function, then you can write simply exit; to exit the program normally. That is:
- exit;
- exit();
- exit(0);
all three statements does the same job, of exiting the program normally.
To print some message before exiting the program, follow the example given below:
<?php $x = 10; for($i=1; $i<$x; $i++) { if($i==4) exit("Exiting..."); echo "The value of \$i is $i"; echo "<BR>"; } echo "The value of \$i is $i"; echo "<BR>"; ?>
The output should be:
The value of $i is 1 The value of $i is 2 The value of $i is 3 Exiting...
« Previous Tutorial Next Tutorial »