Current Location: Home> Latest Articles> How to Use exit to Print Debug Information? Can You View Logs When PHP Script is Terminated?

How to Use exit to Print Debug Information? Can You View Logs When PHP Script is Terminated?

gitbox 2025-07-26

During development, debug information is vital for troubleshooting issues. We usually use echo, var_dump, print_r, and similar methods to print debug information. However, when we encounter errors in the code, we might need to stop the script execution while printing relevant debug details. At this point, the exit function becomes a very important tool, as it can halt the script from continuing. So, how can we use exit to print debug information? Can we still view logs when the script is terminated? This article will explain in detail.

1. Basic Usage of exit

In PHP, the exit() function is used to terminate the current script execution. It can accept a string as an argument to output that message when terminating, or it can return an exit status code:

<span><span><span class="hljs-keyword">exit</span></span><span>(</span><span><span class="hljs-string">"Script terminated with this message"</span></span><span>);
</span></span>

Or just pass an exit status code (usually an integer):

<span><span><span class="hljs-keyword">exit</span></span><span>(</span><span><span class="hljs-number">0</span></span><span>);  </span><span><span class="hljs-comment">// Normal exit</span></span><span>
</span><span><span class="hljs-keyword">exit</span></span><span>(</span><span><span class="hljs-number">1</span></span><span>);  </span><span><span class="hljs-comment">// Abnormal exit</span></span><span>
</span></span>

exit() not only stops script execution but can also return an exit code for external systems or other scripts to reference.

2. Using exit to Print Debug Information

When debugging, you may want to stop execution at a specific point and output debug information. In this case, you can pass debug info as an argument to exit for quick issue localization.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Simulate some code</span></span>
</span><span><span class="hljs-variable">$variable</span></span><span> = </span><span><span class="hljs-string">"Test value"</span></span><span>;
<p></span>// Print debug info here and terminate script<br>
exit("Debug Info: Variable value is {$variable}");<br>
?><br>
</span>

In the code above, when exit() is executed, PHP outputs "Debug Info: Variable value is Test value" and terminates the script. This way, you can output useful info at a specific execution point while ensuring the script stops to prevent unnecessary subsequent code execution.

3. Logging Debug Information

In real development, debug info is often not desired to be output directly in the browser, especially in production environments. To avoid leaking sensitive information, PHP’s error_log function can be used to record debug info into log files. This way, even when the script is interrupted, you can still review debug information in the logs.

3.1 Writing Debug Info to Logs

The error_log() function writes messages to PHP’s error log or a custom log file. Here’s an example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$variable</span></span><span> = </span><span><span class="hljs-string">"Test value"</span></span><span>;
<p></span>// Write debug info to PHP error log<br>
error_log("Debug Info: Variable value is {$variable}", 0);<br>
</span>

In this example, the debug info is written to PHP’s default error log. You can check the error_log directive in your php.ini file to find the log location.

3.2 Writing to a Custom Log File

If you want to write debug info to a custom log file, you can specify the file path:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$variable</span></span><span> = </span><span><span class="hljs-string">"Test value"</span></span><span>;
<p></span>// Write debug info to a specified log file<br>
error_log("Debug Info: Variable value is {$variable}", 3, "/path/to/custom_log.log");<br>
</span>

Here, the log info is written to the /path/to/custom_log.log file instead of the default PHP error log.

4. Combining exit and Logging

You can combine exit with logging by first writing debug info to a log file and then terminating the script with exit():

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$variable</span></span><span> = </span><span><span class="hljs-string">"Test value"</span></span><span>;
<p></span>// Write debug info to log<br>
error_log("Debug Info: Variable value is {$variable}", 3, "/path/to/custom_log.log");</p>
<p>// Terminate script execution<br>
exit("Script terminated, debug info logged.");<br>
?><br>
</span>

This code first writes debug info to the log file, then terminates the script with exit() and displays a message.

5. Considerations During Debugging

When using exit and logging for debugging, keep these points in mind:

  • Use with caution in production: In production, avoid directly outputting debug info with exit(), as it stops the whole page and may expose internal states. It's generally better to write debug info to log files to avoid impacting user experience.

  • Log file permissions: Ensure PHP has permission to write to the log files. Without permission, error_log() will fail to record logs.

  • Clean up debug info: Debug info is helpful during development but should be removed or hidden before deployment to avoid exposing details to users.

  • Manage log files: Large log files can affect server performance. Regularly clean or rotate logs to prevent disk space issues.

6. Conclusion

By combining the use of exit and error_log, PHP developers can flexibly record debug information into log files while interrupting script execution. This approach aids troubleshooting while ensuring unnecessary information isn't exposed in production. Whether in local development or live environments, properly using logging and debug termination is an effective way to improve development efficiency and application stability.