What is the purpose of the exit() function in PHP

The exit() function in PHP is used to terminate the current script and optionally send a message or status code to the calling program. This function is especially useful when you want to stop execution of a script after a certain condition is met or if an error occurs.

Example of the exit() function

<?php // Example of exit() function in PHP $condition = true; if ($condition) { echo "Exiting the script."; exit(); // Terminate the script } echo "This line will not be executed."; ?>

PHP exit function terminate script PHP exit exit example