- 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
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 »
Like/Share Us on Facebook 😋