How can PHP files be protected from direct access using constants?

PHP files can be protected from direct access by defining a constant in each file and checking if that constant is defined before allowing the script to run. This prevents users from accessing the file directly through the browser, ensuring that the file is only accessed through the intended entry points.

<?php
define('MY_APP', true);

if(!defined('MY_APP')){
    die('Direct script access not allowed');
}

// Rest of your PHP code here