致命错误:调用未定义的函数tally()

Ven*_*raj 3 php fatal-error

我有一个PHP测验的以下代码.

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
Run Code Online (Sandbox Code Playgroud)

当我POST将表单数据转换为不同的文件时,即如果上面的代码是在单独的文件中编写的,那么一切正常.

但是当我POST将表单数据写入页面本身时,我得到了

Fatal error: Call to undefined function tally()
Run Code Online (Sandbox Code Playgroud)

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

当我尝试使用时

                $this->tally($_POST[$qvar]);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

Fatal error: Using $this when not in object context
Run Code Online (Sandbox Code Playgroud)

小智 10

这是因为您在尝试调用它后声明了该函数:

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

function tally ($question) // TALLY FUNCTION DECLARATION COMES LATER
Run Code Online (Sandbox Code Playgroud)

我会把它放在前面 if(isset($_POST['submit'])) {或作为内部的第一件事.

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

$ this-> tally将不起作用,因为它不是一个Object,你只是在判断一个函数.