Open In App

PHP | exit( ) Function

Last Updated : 01 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script.
The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.
The message to be displayed is passed as a parameter to the exit() function and it terminates the script and displays the message.
The exit() function is an alias of the die() function.

Syntax:

exit(message)

Parameters Used:
The exit() function in PHP accepts one parameter.

  1. message : It is a mandatory parameter which specifies the message or status number to write before exiting the script.
Return Value:
It does not return any value.

Errors And Exceptions

  1. exit() is a language construct and it can be called without parentheses if no status is passed.
  2. If the status passed as a parameter is an integer, that value will be used as the exit status and not be printed.
  3. Exit statuses should be in the range 0 to 254 and the exit status 255 should not be used since it is reserved by PHP.

Below programs illustrate the exit() function:

Program 1:




<?php
  
// opening a link
fopen($link, "r")
  
//using exit() to display message and terminate script
or exit("Unable to establish a connection to $link");
?>


Output:

Unable to establish a connection to https://www.geeksforgeeks.org

Program 2:




<?php
//declaring variables
$a=5;
$b=5.0;
  
if($a==$b)
 {
    //terminating script with a message using exit()
    exit('variables are equal');
 }
else
 {
   //terminating script with a message using exit()
    exit('variables are not equal'); 
 }
?>


Output:

variables are equal

Reference :
http://php.net/manual/en/function.exit.php

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Similar Reads

Difference between die() and exit() functions in PHP
PHP exit() Function: In PHP, the exit() function prints a message and exits the application. It's often used to print a different message in the event of a mistake. Use exit() when there is not an error and have to stop the execution. Syntax: exit("Message goes here"); or exit(); Example: exit("This request is processed"); Program 1: C/C++ Code
2 min read
D3.js selection.exit() Function
The d3.selection.exit() function in D3.js is used to remove the elements or tags which correspond to the old data or in simple words This is used to update the DIV elements that were created before with the new data given. Syntax: selection.exit(); Parameters: This function does not accept any parameter. Return Values: This function returns the exi
2 min read
Node.js process.exit() Method
The process.exit() method is used to end the process which is running at the same time with an exit code in NodeJS. Syntax: process.exit( code ) Parameter: This function accepts single parameter as mentioned above and described below: Code: It can be either 0 or 1. 0 means end the process without any kind of failure and 1 means end the process with
2 min read
Node.js Process exit Event
The process is the global object in Node.js that keeps track of and contains all the information of the particular node.js process that is executing at a particular time on the machine. The process.exit() method is the method that is used to end the Node.js process. Every process action on the machine or a program is an event. For every event, ther
2 min read
How to exit process in Node.js ?
In this article, we will see how to exit in NodeJS application. There are different types of methods to exit in Nodejs application, here we have discussed the following four methods. Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below:
3 min read
Node.js Exit Codes
Node.js is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Node.js allows developers to utilize JavaScript to create command-line tools and server-side scripting, which involves running scripts on the server before sending the page to the user's brows
3 min read
JavaScript Program to Forced Exit from Recursion
Recursion is a powerful programming technique that involves calling a function to solve a problem in smaller parts. However, during recursive operations, there may be scenarios where we need to forcibly exit the recursion to prevent an infinite loop or avoid unnecessary computations. In this article, we'll explore common situations where forced exi
3 min read
How to Exit after res.send() in Express JS
In this article, we are going to learn how we can exit after the res.send() function in Express JS. In Express the res.send() method is mainly used to send a response to the client with the specified content. This function automatically sets the Content-Type Header which is based on the data provided. We are going to implement the Exit after res.se
3 min read
How to Exit a Loop Before it Completes all Iterations in JavaScript ?
In JavaScript, you can exit a loop before it completes all iterations using the break statement. The break statement is used to terminate the loop prematurely based on a certain condition. Example: Here, the loop prints numbers from 1 to 10. However, when i becomes equal to 5, the break statement is executed, causing the loop to terminate immediate
1 min read
How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read
Article Tags :