我的脚本运行时间太长了。有可能我有太多if语句嵌套了吗?

时间:2013-04-08 07:14:34

标签: php if-statement directory

我是编程新手,我不得不自学。但是,我无法弄清楚我在这个项目中做错了什么。这个项目适合我自己,用PHP编写快捷方式,所以我不需要编写那么多行代码;当我编程时,有时我会使用相同的20行进行不同的应用程序。

这是我的剧本:

function writeDir($directory = null, $link = null, $exception = null) {
    if (!isset($directory)) {
        $directory = './';
    }
    if (!isset($link)) {
        $link = false;
    }
    if (!isset($exception)) {
        $exception = null;
    }
    // now...
    if ($link==true&&$exception!=null) { // do a link and exception(s)
        $directoryList = scandir($directory); // get an array of all directory items
        $directoryLength = count($directoryList); // count $directoryList array length
        if (is_string($exception)) { // if one (1) exception
            for ($i=2; $i<$directoryLength; $i++) { // cycle through all directory items
                if ($directoryList[$i] == $exception) { // if we hit that one (1) exception
                    echo ''; // do nothing
                } else {
                    echo '<a href="' . $directoryList[$i] . '"> ' . $directoryList[$i] . ' </a><br />' . "\n";
                }
            }
        }
        if (is_array($exception)) { // if multiple exceptions
            $exceptionList = count($exception); // count $exception array length
            for ($i=2; $i<$directoryList; $i++) { // cycle through all directory items
                for ($j=0; $j<$exceptionList; $j++) { // cycle through all exceptions
                    if ($directoryList[$i] == $exceptionList[$j]) { // if we hit one of the multiple exceptions
                        echo ''; // do nothing
                    } else {
                        echo '<a href="' . $directoryList[$i] . '"> ' . $directoryList[$i] . ' </a><br />' . "\n";
                    }
                }
            }
        }
    }
    if ($link==true&&$exception==null) {
        $directoryList = scandir($directory);
        $directoryLength = count($directoryList);
        for ($i=2; $i<$directoryLength; $i++) {
            echo '<a href="' . $directoryList[$i] . '"> ' . $directoryList[$i] . ' </a><br />' . "\n";
        }
    }
    if ($link==false&&$exception!=null) { // do only exception(s) without links
        $directoryList = scandir($directory); // get an array of all directory items
        $directoryLength = count($directoryList); // count $directoryList array length
        if (is_string($exception)) { // if one (1) exception
            for ($i=2; $i<$directoryLength; $i++) { // cycle through all directory items
                if ($directoryList[$i] == $exception) { // if we hit that one (1) exception
                    echo ''; // do nothing
                } else {
                    echo $directoryList[$i] . '<br />' . "\n";
                }
            }
        }
        if (is_array($exception)) { // if multiple exceptions
            $exceptionList = count($exception); // count $exception array length
            for ($i=2; $i<$directoryList; $i++) {
                for ($j=0; $j<$exceptionList; $j++) {
                    if ($directoryList[$i] == $exceptionList[$j]) {
                        echo '';
                    } else {
                        echo $directoryList[$i] . '<br />' . "\n";
                    }
                }
            }
        }
    }
    if ($link==false&&$exception==null) {
        $directoryList = scandir($directory);
        $directoryLength = count($directoryList);
        for ($i=2; $i<$directoryLength; $i++) {
            echo $directoryList[$i] . '<br />' . "\n";
        }
    }
}

我知道它看起来很多。但是,我正在努力让我的生活更简单。

基本上,在PHP文件中调用时的语法是:

writeDir(); // at the very least:或:writeDir('./', true, 'index.php');

第二个文件在当前目录中写入文件,并附带相应的链接,但跳过index.php文件。

第三个(第三个)参数可以是单个省略页面(作为字符串),也可以是多个省略页面的数组。或者,至少那是我想要实现的目标。

当然,在我使用它的所有页面上都需要包含此源文件。以前,当IT WAS工作时,我只能格式化目录项列表而不包含一(1)个文件。所以,我试图允许一组文件。现在,它永远加载。我最后不得不使用set_time_limit()函数退出脚本。

现在,最后一件事。当我使用set_time_limit()函数时,它显示了我的目录项,但由于某种原因,所有内容中的两(2)个......我做错了什么?

是否有太多if语句,或者我忽略了什么?我应该重新开始吗?

我只编程(非专业)约5年,所以我真的不知道我在做什么。任何指导都将不胜感激。

希望我已经提供了足够的信息和细节。

JDot

P.S。 任何想要使用这个脚本的人都非常欢迎(如果它甚至值得使用)。

1 个答案:

答案 0 :(得分:0)

如果

,请使用else
if($a==1)
{
echo $a;
}
elseif($b==1)
{
echo $b;
}
else
{
echo "c";
}

并且有单个if else语句的三元运算符:学习它

相关问题