// Do whatever you're doing to check
// If the check fails, PHP sends the 404:
header("HTTP/1.0 404 Not Found");
// Then display your custom error document with PHP
// You can display the other contents of error404.php
echo "Oops, page wasn't found!";
And remove the ErrorDocument directive from your Apache configuration.
The following does not take place in error404.php, but rather in a normal script like index.php:
The thing is that the "page" is like link and this is how it is described. There will be a lot of those fake pages that are dynamically created. I've seen a lot of website designers on forums complaining of getting a 200 OK with ErrorDocument 404 instead of 404 not found and I getting the opposite. I want a 200 OK instead of a 404.
When Apache sends the document specified by ErrorDocument, it is not serving it as a regular page, and has already sent the 404 headers. Instead of using the ErrorDocument directive in Apache, you should instead use your PHP script to check first if the document exists, and if it does, display it. If it does not exist, then PHP sends the 404 error header itself.