致命错误:调用未定义的函数tally() [英] Fatal error: Call to undefined function tally()

查看:124
本文介绍了致命错误:调用未定义的函数tally()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下php测验代码.

I have the following code for a php quiz.

if(isset($_POST['submit'])) {

    //Check to make sure that the name field is not empty
        if(($_POST['q_1'] == '') ||  ($_POST['q_2'] == '') || ($_POST['q_3'] == '') || ($_POST['q_4'] == '') || ($_POST['q_5'] == '')) {
            $nameError = 'Please choose an option';
            $hasError = true;
        }
        else {
                for ($i=1; $i<=$types; $i+=1)
                    {
                        $nowval[$i] = 0;
                    }
                for ($i=1; $i<=$questions; $i+=1)
                    {
                        $qvar = "q_$i";
                        //echo $qvar;
                        tally($_POST[$qvar]);// LINE THAT CAUSES ERROR
                    }
                $dominant = 1;
                $domval = $nowval[1];
                for ($i=2; $i<=$types; $i+=1)
                    {
                        if ($domval < $nowval[$i])
                            {
                                $dominant = $i;
                                $domval = $nowval[$i];
                            }
                    }
                function tally ($question) // TALLY FUNCTION
                    {   
                        global $nowval;
                        $nowval[$question]++;
                    }
                if (is_file("$quiz.rsl"))
                    {
                        $fp = fopen("$quiz.rsl", 'r');
                        $line = fgets($fp, 1024);
                        fclose($fp);
                        $people = explode("|", $line);
                        $people[$dominant-1] += 1;
                        $timestaken = 0;
                        foreach($people as $tally)
                            {
                                settype($tally, 'integer');
                                $timestaken += $tally;
                            }
                        $fp = fopen("$quiz.rsl", 'w');
                        for($i=0; $i<$types; $i++)
                            {
                                fwrite($fp, $people[$i]."|");
                            }
                        fclose($fp);
                    }
                else
                    {
                        for($i=0; $i<$types; $i++)
                            {
                                $people[] = 0;
                            }
                        $people[$dominant-1] += 1;
                        $timestaken = 1;
                        $fp = fopen("$quiz.rsl", 'w');
                        for($i=0; $i<$types; $i++)
                            {
                                fwrite($fp, $people[$i]."|");
                            }
                        fclose($fp);
                    }
                $percentage = ($people[$dominant-1] / $timestaken) * 100;
                $dec=2;
                $format="%.$dec" . "f";  
                $number=sprintf($format,$percentage);
                $percentage=strtok($number,".");
                $dc=strtok(".");   
                if ($dec!=0) 
                    { 
                        $percentage = "$percentage" . ".$dc";
                    } 

                $emailSent = true;
    }//else
}//main if

当我POST将表单数据保存到另一个文件中时,即,如果上述代码写在一个单独的文件中,则一切正常.

When I POST the form data to a different file ie, if the above code is written in a separate file, it all works fine .

但是当我POST将表单数据发送到页面本身时,我得到

but when I POST the form data to the page itself, I am getting

Fatal error: Call to undefined function tally()

我不了解此问题的根本原因.

I am not understanding the root cause of this problem.

当我尝试使用

                $this->tally($_POST[$qvar]);

我遇到以下错误.

Fatal error: Using $this when not in object context

推荐答案

之所以会发生,是因为您在尝试调用该函数后声明了该函数:

It happens because you are declaring the function after trying to call it:

tally($_POST[$qvar]);// LINE THAT CAUSES ERROR

function tally ($question) // TALLY FUNCTION DECLARATION COMES LATER

我会把任何一个放在 if(isset($_POST['submit'])) {或其中的第一件事.

I would put either before if(isset($_POST['submit'])) { or as the first thing inside.

   if (!function_exists('tally')) {
   function tally ($question) // TALLY FUNCTION
       {   
            global $nowval;
            $nowval[$question]++;
       }   
  }

$ this-> tally无效,因为它不是对象,您只是对一个函数进行了小数压缩.

$this->tally won't work because it's not an Object, you are justing decrating a function.

这篇关于致命错误:调用未定义的函数tally()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆