PHP: exit(0) or exit(1)

Oezhan D.
1 min readJan 31, 2023

--

exit(0) and exit(1) are used in programming to indicate the success or failure of a script.

In most cases, exit(0) is used to indicate a successful exit and exit(1) to indicate an error or unsuccessful exit. These values can be used to check the success of a script in a shell script or in other scripts that call the first script.

For example, if a script exits with exit(0), the shell script that calls it can assume that the script executed successfully and move on to the next command. If it exits with exit(1), the shell script can assume that an error occurred and take appropriate action, such as logging the error or attempting to recover from the error.

<?php

$file = '/path/to/file';

if (!file_exists($file)) {
echo "Error: File does not exist\n";
exit(1);
}

$contents = file_get_contents($file);
if ($contents === false) {
echo "Error: Unable to read file contents\n";
exit(1);
}

echo "File contents:\n";
echo $contents;

exit(0);

In general, exit(0) means that the script executed without errors and exit(1) means that there was an error during execution. The exact meaning of these values may vary depending on the environment and the application.

--

--