关联数组foreach循环以加载所需的文件

时间:2013-07-05 16:45:39

标签: php arrays foreach

我正在尝试使用关联数组加载'config.php'中的所有文件。不同的值表示可以找到文件的位置。但是我一直收到错误输出:

  

无法加载:array.php,array.php,array.php,array.php,dir.php,   dir.php,dir.php,dir.php,auth.php,auth.php,auth.php,auth.php,   forms.php,forms.php,forms.php,forms.php,image.php,image.php,   image.php,image.php,navigation.php,navigation.php,navigation.php,   navigation.php,stats.php,stats.php,stats.php,stats.php,   tables.php,tables.php,tables.php,tables.php,text.php,text.php,   text.php,text.php,user.php,user.php,user.php,user.php,   youtube.php,youtube.php,youtube.php,youtube.php,messages.php,   messages.php,messages.php,messages.php,nemesis.php,nemesis.php,   nemesis.php,nemesis.php,upload.php,upload.php,upload.php,   upload.php,construct.php,construct.php,construct.php,   construct.php,prepared_arrays.php,prepared_arrays.php,   prepared_arrays.php,prepared_arrays.php

我不确定为什么会发生这种情况,也不知道为什么它说文件无法加载4次。

<?php
    $require = array(
        'array.php' => 'f',
        'dir.php' => 'f',
        'auth.php' => 'f',
        'forms.php' => 'f',
        'image.php' => 'f',
        'navigation.php' => 'f',
        'stats.php' => 'f',
        'tables.php' => 'f',
        'text.php' => 'f',
        'user.php' => 'f',
        'youtube.php' => 'f',
        'messages.php' => 'c',
        'nemesis.php' => 'c',
        'upload.php' => 'c',
        'construct.php' => 't',
        'prepared_arrays.php' => 'l'
    );

    load($require);

    function load($require) {
        foreach ($require as $filename => $directory) {
            // init functions
            if ($directory = 'f') {
                $prep = FUNCTIONS_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
            // init classes
            if ($directory = 'c') {
                $prep = CLASSES_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
            // init templates
            if ($directory = 't') {
                $prep = TEMPLATES_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
            // init libs
            if ($directory = 'l') {
                $prep = LIBS_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
        }
        if (!empty($fail)) {
            if (!empty($pass)) {
                $passed  = implode(', ', $pass);
                $message = "Loaded: {$passed}";
                echo $message;
            }
            if (!empty($fail)) {
                $failure = implode(', ', $fail);
                $message = "Could not load: {$failure}";
                echo $message;
            }
            exit();
        }
    }
    ?>

1 个答案:

答案 0 :(得分:4)

$directory = 'l' $directory == 'l' :)

PS 同样适用于所有比较。它们是比较,而不是作业。