调用函数错误

时间:2014-10-12 15:26:42

标签: php

Linux系统

寻求我的功能帮助。我是新学的。有人提到创建一个函数来防止编写相同的代码太多次了。

我找到了许多关于他们的网页,他们帮助了我,但我仍然遇到让我上班的问题。

它没有给出任何日志错误,也没有创建任何内容。从一开始我总是会出错,所以我知道我错过了什么。

这是我到目前为止所拥有的。

提前谢谢。

鲍勃

<?php

$dirPath = "Path to File Location";
$buildPath = "Path to File Location";

function createFiles() {
    for ($i = 1; $i < 45; ++$i) {
        $filename = $buildPath . '/$area'. sprintf("%02s", $i) . '.php';
        if (!file_exists($buildPath$filename)) {
            $myfile = fopen($buildPath$filename, "w") or die("Unable to open item$i file!"); 
            fwrite($myfile, $txt1);
            fwrite($myfile, $addtitle);
            fwrite($myfile, $incltxt);
            fclose($myfile);
            chmod("$buildPath"."$area"."$i".".php", 0755);
        }
        // fopen, fwrite, fcloses...
        // filename for delete page setup
        if (!file_exists("$dirPath"."delete.page.php")) {
            $myfile = fopen("$dirPath"."delete.page.php", "w") or die("Unable to open file!");
            $txt = "<option value=\"\">Select Page to Delete......</option>\n
                    <option value=\"generalinfo.tx\">General Information Page.</option>\n
                    <option value=\"$filename\">$area Pg. $i</option>\n";
            fwrite($myfile, $txt);
            fclose($myfile);
            chmod("$dirPath"."delete.page.php", 0755);
        }
        else
            {
                $myfile = fopen("$dirPath"."delete.page.php", "a") or die("Unable to open file!");
                $txt = "<option value=\"$filename\">$area Pg. $i</option>\n";
                fwrite($myfile, $txt);
                fclose($myfile);
                chmod("$dirPath"."delete.page.php", 0755);
            }
            break;
    }
    return true;
}


    if ($i == 1) {
        createFiles();
    }
    // Once we get more than 4 items written in each file we need a new file.
    // This can happen maximum 11 times for each session
    if ($i == 5) {
        createFiles();
    }
    if ($i == 9) {
        createFiles();
    }
    if ($i == 13) {
        createFiles();
    }
    if ($i == 17) {
        createFiles();
    }
    etc......
    if($i == 49) {
        echo "<br />Maximum of 12 pages hit. Time to delete a few";
    }

?>

2 个答案:

答案 0 :(得分:2)

  

$i如果它等于,那部分正在运作。

如果该部分有效,那么您没有显示所有代码!

举个例子,这与你正在做的事情基本相同:

function createFiles()
 {
  $i = 3;
 }


if ($i == 3)
 {
  createFiles();
  echo "is 3";
 }
else
 {
  echo "is not 3";
 }

这将始终回应“不是3”。未设置var,因为在函数中设置的是A)不在同一全局范围内,并且B)该函数甚至从未被调用过。

如果函数内的var被设置为某个东西,那么如果函数内部的函数被设置为某个函数,那么函数内的函数如何在函数外部的代码中确定是否可以调用该函数? 它抓到了22.

您的函数调用createFiles()仅在$i == 1时执行,并且您发布的代码未设置该var,它将始终为null或未设置(等)。
这意味着调用该函数的if语句永远不会工作,并且总是返回FALSE,因此永远不会调用该函数。

您还必须了解全局范围,因为函数内的变量保留在函数内,并且函数外部的任何内容都是独立的。
从本质上讲,你目前有两个变量,一个在函数内部,一个在它外面,虽然它们的名字$i它们的全局范围都没有结合,所以它们永远不会相互交互。

您可以将var传递给函数,以允许函数外部的变量及其数据在函数内使用,并从函数中返回一个函数以使用函数外部的数据。

此外,每次用户使用您的代码时,任何变量都将被重置,这意味着您要确定他们是否已经使用了X次的if将无法按预期工作。
你不能做这个客户端。

您可以设置会话或Cookie,但客户可以根据需要轻松绕过它(清理浏览器数据) 如果你真的想要限制它们,你将需要一个客户端无法访问的持久数据存储方法 - 文件或更实用的方法来增加数据库值。
然后,如果他们可以在第二天再次使用该表单,则可以通过表格运行cron并根据需要进行清理。

修改
要使脚本正常工作,请尝试引入几个阶段以简化操作。

Stage1:让函数独立工作,添加文件(或其目的是什么) Stage2:操纵你想要的函数 - 即只有当用户使用了函数时才调用函数&lt; 10(等)。

你甚至可以将stage1分成几个子阶段,在Stage1a中,让函数访问正确的目录; Stage1b,获取访问文件的功能; Stage1c,获取函数写入文件等。

尽量避免一次性编写具有众多功能区域的脚本,并将其分解为可管理的逻辑块。这样做,也可以帮助您看到不同问题之间的分离。

答案 1 :(得分:0)

这是我更新的代码。在我放弃之前,我又尝试了一件事,这是我添加的一条额外的行和我做的更改。

愚蠢的错误。我不知道你们怎么能一直这样做。这是我第一次搞砸php中的一个函数并花了一整天时间。但它正在发挥作用,所以我想我会分享所有人的变化。

function createFiles() {

extract($GLOBALS);

     for ($n = 1; $n < 45; ++$n) {
        $filename = $buildPath . $area . sprintf("%02s", $n) . '.php';
        $filenameb = $area . sprintf("%02s", $n) . '.php';
                                    //^ length of number you need, 2 in this case
        if (!file_exists($filename)) {
        $myfile = fopen($filename, "w") or die("Unable to open item$i file!"); 
        fwrite($myfile, $txt1);
        fwrite($myfile, $addtitle);
        fwrite($myfile, $incltxt);
        fclose($myfile);
        chmod("$filename", 0755);
     }
      break;
     }
if (!file_exists("$dirPath"."delete.page.php")) {
    $myfile = fopen("$dirPath"."delete.page.php", "w") or die("Unable to open file!");
    $txt = "<option value=\"\">Select Page to Delete......</option>\n
            <option value=\"generalinfo.php\">General Information Page.</option>\n
            <option value=\"$filenameb\">$area Pg. $n</option>\n";
    fwrite($myfile, $txt);
    fclose($myfile);
    chmod("$dirPath"."delete.page.php", 0755);
}
else
    {
    $myfile = fopen("$dirPath"."delete.page.php", "a") or die("Unable to open file!");
    $txt = "<option value=\"$filenameb\">$area Pg. $n</option>\n";
    fwrite($myfile, $txt);
    fclose($myfile);
    chmod("$dirPath"."delete.page.php", 0755);
    }
return true;

if ($i == 1) {
    createFiles();
}
// Once we get more than 4 items written in each file we need a new file.
// This can happen maximum 11 times for each session
if ($i == 5) {
    createFiles();
}
if ($i == 9) {
    createFiles();
}
if ($i == 13) {
    createFiles();
}
if ($i == 17) {
    createFiles();
}
etc......
if($i == 49) {
    echo "<br />Maximum of 12 pages hit. Time to delete a few";