此行出错:$an = explode(";", $f[$i]);
这个也是:if ($wasone)
(未定义的变量)有什么帮助吗?非常感谢。
<?
if ($_POST["submit"])
{
$a = answer();
$out = "Q: $ask<br>A: ".$a;
$tile = ($cfg["scrolling"]) ? $tile : "";
echo "$out<br>$tile";
echo "<input name='tile' type='hidden' id='tile' value='$out<br>$tile'>";
}
// answers
function answer()
{
global $cfg, $ask;
$ask = (empty($ask)) ? "<empty>" : $ask;
$kick = array("?","'n");
$ask = str_replace($kick,"",$ask);
$f = file($cfg["answerswersfile"]);
for ($i=0; $i<=count($f); $i++)
{
$an = explode(";", $f[$i]);
$a = $an[0];
if (strstr($a,trim($ask)))
{
if ($wasone)
{
return("Please be more concrete");
}
array_shift($an);
array_pop($an);
$ai = rand(0, count($an)-1);
// answering
$wasone = true;
$retval = $an[$ai];
}
}
$retval = (empty($retval)) ? "I dont understand you. Please try again." : $retval;
return $retval;
}
?>
for循环中的条件应该是
$count = count($f);
for ($i=0; $i<$count; $i++)
不使用"="以确保只有索引访问范围从0到count-1
行
for ($i=0; $i<=count($f); $i++)
应该是
for ($i=0; $i<count($f); $i++)
count()返回$f的元素数,它比$f最后一个元素的索引多一个(在本例中为九个)。您希望在$i超过最后一个元素的索引之前停止
默认情况下,数组索引从0开始。如果是count($f) === 9
,则表示您的数组中有索引0, 1, 2 ... 8
。如果在$i <= 9
时循环,则将尝试访问索引为9的元素。。。它不在那里。