What are the potential pitfalls of directly accessing PHP files in a website and how can this be prevented for better security measures?

Directly accessing PHP files in a website can expose sensitive information, such as database credentials or other critical data. To prevent this, you can use a .htaccess file to restrict direct access to PHP files by denying access from external sources.

# Prevent direct access to PHP files
<Files *.php>
    Order Deny,Allow
    Deny from all
</Files>