How can using array_key_exists in PHP code help in handling include statements more efficiently?

Using array_key_exists in PHP code can help in handling include statements more efficiently by checking if a file has already been included before including it again. This prevents duplicate includes and potential conflicts that may arise from including the same file multiple times.

$included_files = [];
function include_once_custom($file){
    global $included_files;
    if(!array_key_exists($file, $included_files)){
        include $file;
        $included_files[$file] = true;
    }
}

include_once_custom('file1.php');
include_once_custom('file2.php');
include_once_custom('file1.php');