Php 调用函数错误
Linux系统 正在寻找有关我的功能的帮助。我是新来的。有人提到要创建一个函数,以避免多次编写相同的代码 我已经找到了很多关于它们的页面,它们对我很有帮助,但我在如何让我的页面正常工作方面仍然存在问题 它没有给出任何日志错误,也没有创建任何内容。我总是在开始时出错,所以我知道我错过了一些东西 这是我到目前为止所拥有的 先谢谢你 鲍勃Php 调用函数错误,php,Php,Linux系统 正在寻找有关我的功能的帮助。我是新来的。有人提到要创建一个函数,以避免多次编写相同的代码 我已经找到了很多关于它们的页面,它们对我很有帮助,但我在如何让我的页面正常工作方面仍然存在问题 它没有给出任何日志错误,也没有创建任何内容。我总是在开始时出错,所以我知道我错过了一些东西 这是我到目前为止所拥有的 先谢谢你 鲍勃 $i是,如果它等于,则该部分正在工作 如果该部分工作正常,那么您没有显示所有代码 以这个与您所做的基本相同的示例为例: function createFiles
$i
是,如果它等于,则该部分正在工作
如果该部分工作正常,那么您没有显示所有代码
以这个与您所做的基本相同的示例为例:
function createFiles()
{
$i = 3;
}
if ($i == 3)
{
createFiles();
echo "is 3";
}
else
{
echo "is not 3";
}
这将始终呼应“不是3”。未设置变量,因为函数中设置的变量A)不在同一全局范围内,B)函数甚至从未被调用过
如果函数中的变量设置为某个值,那么函数中正在操作的变量如何在函数外部的代码中确定是否应该调用同一个函数?这是第22条军规 函数调用
createFiles()
仅在$i==1
时执行,并且您发布的代码未设置该变量,它将始终为null或未设置(等)。这意味着调用函数的
if
语句永远不会工作,并且总是返回FALSE
,因此函数永远不会被调用
您还必须了解全局范围,因为函数内的变量位于函数内,而函数外的任何变量都是独立的。实际上,您当前有两个变量,一个在函数内部,另一个在函数外部,尽管它们仍然都被命名为
$i
,但它们的全局范围并不匹配,因此它们从不相互交互
您可以将变量传递到函数中,以允许在函数中使用函数外的变量及其数据,并从函数中返回一个变量以使用函数外的数据
此外,每次用户使用您的代码时,任何VAR都将被重置,这意味着您用来确定他们是否使用了X次的if
s组将无法正常工作。您不能在客户端执行此操作 您可以设置会话或cookie,但如果客户机愿意,可以轻松绕过它(清理浏览器数据)。
如果您真的想限制它们,您将需要一种客户端无法访问的持久数据存储方法—或者是一个文件,或者更可靠的方法来增加数据库值。
然后,如果他们第二天可以再次使用该表单,cron就可以遍历该表并根据需要进行清理 编辑
要使脚本正常工作,请尝试引入几个阶段使其简单化 阶段1:让函数自己工作,添加文件(或任何其用途)。
阶段2:按照您的意愿操作被调用的函数-即,仅当用户使用函数<10(等)时才调用函数 您甚至可以将stage1分成几个子阶段,在Stage1a中,让函数访问正确的目录;阶段1b,获取函数访问文件;阶段1c,将函数写入文件等
尽量避免一次性编写一个包含大量功能的脚本,并将其分解为可管理的逻辑块。这样做还将帮助您看到不同关注点之间的分离 这是我的更新代码。在我打算放弃之前,我又尝试了一件事,下面是我添加的一行内容和我所做的更改 愚蠢的错误。我不知道你们怎么能一直这样。这是我第一次用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";
函数createFiles(){
提取物(全球);
对于($n=1;$n<45;++$n){
$filename=$buildPath.$area.sprintf(“%02s”,$n)。'.php';
$filenameb=$area.sprintf(“%02s”,$n)。'.php';
//^所需数字的长度,在本例中为2
如果(!file_存在($filename)){
$myfile=fopen($filename,“w”)或die(“无法打开项目$i文件!”);
fwrite($myfile,$txt1);
fwrite($myfile,$addtitle);
fwrite($myfile,$incltxt);
fclose($myfile);
chmod(“$filename”,0755);
}
打破
}
如果(!file_存在(“$dirPath.”delete.page.php”)){
$myfile=fopen($dirPath“、delete.page.php”、“w”)或die(“无法打开文件!”);
$txt=“选择要删除的页面…”\n
常规信息页。\n
$area Pg.$n\n“;
fwrite($myfile,$txt);
fclose($myfile);
chmod(“$dirPath”,“delete.page.php”,0755);
}
其他的
{
$myfile=fopen($dirPath“、delete.page.php”、“a”)或die(“无法打开文件!”);
$txt=“$area Pg.$n\n”;
fwrite($myfile,$txt);
fclose($myfile);
chmod(“$dirPath”,“delete.page.php”,0755);
}
返回true;
如果($i==1){
createFiles();
}
//一旦我们在每个文件中写入超过4项,我们就需要一个新文件。
//每次会话最多可发生11次
如果($i==5){
createFiles();
}
如果($i==9){
createFiles();
}
如果($i==13){
createFiles();
}
如果($i==17){
createFiles();
}
等
如果($i==49){
echo“
最多命中12页,需要删除几页”;
你会遇到什么错误?$buildPath$filename
到$buildPath.$filename
你有什么迹象表明这不符合预期?你做了什么尝试来缩小问题的范围?看起来你已经完成了腿部工作、阅读、尝试、测试等,你只是没有告诉我们你尝试了什么以及发生了什么。主要是,上面的代码到底出了什么问题?没有返回预期值?函数什么都不做?它没有打开、写入、什么都不做。日志中没有错误。我对缺少的内容感到迷茫。不确定还有什么
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";