What are the potential pitfalls of using ErrorDocument for handling errors in PHP?

Using ErrorDocument for handling errors in PHP can lead to potential pitfalls such as not being able to access PHP variables or functions within the error document, making it difficult to provide dynamic error messages. To solve this issue, it is recommended to use PHP's header function to set the appropriate HTTP status code and then include a PHP file with the error message.

<?php
// Set the appropriate HTTP status code
http_response_code(404);

// Include a PHP file with the error message
include('404_error.php');
?>