Php 未定义的偏移量:9,未定义的变量
此行出错:Php 未定义的偏移量:9,未定义的变量,php,Php,此行出错:$an=explode(“;”,$f[$i]) 这个也是:if($wasone)(未定义变量) 有什么帮助吗?多谢各位 <? if ($_POST["submit"]) { $a = answer(); $out = "Q: $ask<br>A: ".$a; $tile = ($cfg["scrolling"]) ? $tile : ""; echo "$out<br>$t
$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["answersfile"]);
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++)
$count=count($f);
对于($i=0;$i行
for ($i=0; $i<=count($f); $i++)
for($i=0;$i默认情况下数组索引从0开始===9
,则表示您所在的数组中有索引0、1、2…8
。如果您在$i时循环,谢谢,但是我应该如何处理其他错误?未定义的变量?您应该在for之前将其初始化为falseloop@msonsona请参阅“计数循环”下的“为什么您的方法不是一个好的实践”部分。基准点的49000%差异可能会调整你的思维。@Blake绝对同意,我的解决方案中没有赌性能…要编辑,thx!@KristinaFiedalan如果你在“for”循环之前初始化变量,我看不出之后在循环中如何定义它。在循环中每次计数都是非常糟糕的做法ce,所以:对于($i=0,$c=count($f);$i<$c;$i++)
Kristina,请参见我在msonsona的答案下的评论。这可能对未来的PHP脚本设计有深刻的启示。
for ($i=0; $i<count($f); $i++)