Syntax:
The exit() function can be called in two ways. It can take one argument only and the argument value can be string or integer. It returns nothing.
If the exit() function of the above syntax is used then the function can be called without any argument or the argument value will be a string that will be printed before terminating the script.
or
If the exit() function of the above syntax is used then the function will be called with an integer value.
Different Uses of Exit() Function
The uses of the exit() function to terminate from the script based on various conditions have been shown in this part of this tutorial.
Example-1: Terminate the script when unable to open a file for reading
Create a PHP file with the following script to open a file for reading and terminate from the script if an error occurs to open the file, otherwise the content of the file will be printed. The filename will be taken from the URL query parameter. If the taken filename by the URL query parameter exists and has the read permission then the content of the file will be printed line by line in the browser.
<?php
//Check the filename has given in the url or not
if(isset($_GET['n']))
{
//Read the filename from url query parameter
$filename = $_GET['n'];
//Open the file for reading if the exists or terminate from the script
$fh = fopen($filename, 'r')
or exit("File does not exist or unable to open the file.");
//Print the contet of the file line by line
echo "The content of the file is:<br />";
while(!feof($fh)) {
echo fgets($fh) . "<br>";
}
}
else
echo "Filename has not given.";
?>
Output:
The following output will appear if the above script is executed without any URL query parameter.
The following output will appear if an existing filename is given in the URL query parameter.
The following output will appear if the filename given in the URL query parameter does not exist or does not have the read permission.
Example-2: Use of multiple exit() functions for reading file
Create a PHP file with the following script that will check the existence of the file, the size of the file, and print the content of the file if the file is not empty. The filename will be taken from the URL query parameter like the previous example. If the taken filename by the URL query parameter does not exist then the exit() function will terminate the script by showing an error message. If the file exists but the size of the file is zero then the exit function will terminate from the script with the number 0. If the file contains data then the full content of the file will be printed by using the file_get_contents() function and the exit() function without any value will terminate the script.
//Check the filename has given in the url or not
if(isset($_GET['n']))
{
//Read the filename from url query parameter
$filename = $_GET['n'];
echo "<center>";
if(!file_exists($filename))
//exit with message
exit("<h3>File does not exist.</h3>");
else if (filesize($filename) == 0)
{
echo "<h3>File is empty.</h3>";
//exit with number
exit(0);
}
else
{
echo "<h4>The content of the file is:</h4>". file_get_contents($filename)."<br />";
//exit without any value
exit();
}
echo "</center>";
}
echo "<center>Filename has not given.</center>";
?>
Output:
The following output will appear if the above script is executed with an existing non-empty filename.
The following output will appear if the above script is executed with an existing empty filename.
Example-3: Terminate the script for the invalid URL address
Create a PHP file with the following script to open an URL address for reading and terminate from the script using the exit() function if the error occurs to open the URL address. The content of the valid URL address will be printed. The URL address will be taken from the URL query parameter like the previous examples.
//Check the url address has given in the url or not
if(isset($_GET['url']))
{
//Read the url address from url query parameter
$url = $_GET['url'];
//Open the url for reading if it exists or terminate from the script
$ln = fopen($url, 'r') or exit("<h3>Invalid URL address.</h3>");
//Store the content of the URL address into a variable
$content = "";
while (!feof($ln))
{
$content .= fread($ln,100000000);
}
fclose($ln);
//Print the content of the URL address
echo $content;
}
else
echo "<h3>URL address has not given.</h3>";
?>
Output:
The following output will appear after executing the above script with a valid URL address, https://www.google.com.
Example-4: Destroy the object variable using the exit() function
Create a PHP file with the following script to check how the destructor method of a class is called when the exit() function is called after creating the object of the class. Here, a class with two public variables, a constructor method, and a destructor method has been defined. An object of the class has been declared to initialize the class variable. When the exit() function will be executed then the destructor method will be called automatically to destroy the object. The last statement of the script will not be executed for the exit() function.
//Define a class with contructor and destructor
class Customer
{
public $name;
public $email;
public function __construct($name, $email)
{
//Initialize the class variables
$this->name = $name;
$this->email = $email;
}
public function __destruct()
{
echo "<h4>Destructor function has called.</h4>";
}
}
//Declare an object of the class
$objCustomer = new Customer("Minhaz kazi", "[email protected]");
//Print the properties of the object
echo "Customer name:".$objCustomer->name."<br />";
echo "Customer email:".$objCustomer->email."<br />";
//Call exit function
exit();
echo 'Terminate from the script.';
?>
Output:
The following output will appear after executing the above script.
Conclusion
The exit() function is a very useful function of PHP to terminate from the script on certain conditions by providing necessary error messages for the users. The various uses of this function have been explained in the tutorial by multiple examples to help the PHP users.