gpt4 book ai didi

php - fatal error : Call to undefined function tally()

转载 作者:可可西里 更新时间:2023-10-31 23:56:54 28 4
gpt4 key购买 nike

我有以下用于 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

当我POST 表单数据到一个不同的文件时,即,如果上面的代码写在一个单独的文件中,它一切正常。

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

Fatal error: Call to undefined function tally()

我不明白这个问题的根本原因。

当我尝试使用

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

我收到以下错误。

Fatal error: Using $this when not in object context

最佳答案

发生这种情况是因为您在尝试调用函数后才声明该函数:

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

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

我会把其中一个放在前面if(isset($_POST['submit'])) { 或作为里面的第一件事。

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

$this->tally 不会工作,因为它不是一个对象,你只是在 decrating 一个函数。

关于php - fatal error : Call to undefined function tally(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23245854/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com