Rich Darvins Rich Darvins - 5 months ago 15
PHP Question

How to tell if a file has already been required?

I've created a php global file (globs.php), and have required it in all of my pages.
However, some pages are now including other pages, and I'm getting an error when it tries to require globs.php again.

How can I tell if a file has been required? That way, I could do if !required('globs.php') require('globs.php').

Answer

You can also make your own require_file function:

<?php
function require_file($file_path){
    static $required_files=array();
    if(!isset($required_files[$file_path])){
        require $file_path;
        $required_files[$file_path]=true;
        return true;
    }
    return false;
}
?>