的“未定义指数”通知弹出,当您使用尚未设置直到那时数组索引值:
<?php
$myarray = array('a'=>1, 'b'=>2);
var_dump($myarray['a']); //outputs int(1) as this is defined
var_dump($myarray['c']); //we defined 'a' and 'b' but not 'c'.
?>
第三行会给:“通知:未定义指数:C在第3行C:\ xampp \ htdocs \ test.php“。
这是通常发生在访问$ _GET或$ _POST数组时。大多数情况下,原因是您拼错了索引(例如,您输入了$_POST['tset']
而不是$_POST['test']
),或者因为您在提交信息的HTML表单中编辑了<input>
元素,然后忘记重新调整PHP代码。
您可以通过是否存在索引使用isset()
像这样的测试以确保一切工作正常:
if(isset($_POST['test'])) {
$myvar = $_POST['test'];
//and then whatever else you intended
}
else {
//the index wasn't defined - you made a mistake, or possibly the user deliberately removed an input from the submitting form
$error = "POST-array is missing the index 'test'!";
//and now make sure that things after this which rely on the 'test' value don't run
}
一个非常普遍的线,你将在脚本的很多发现:
$myvar = isset($_POST['test']) ? $_POST['test'] : 'N/A';
这使用if-else结构的特殊PHP速记。这条线几乎完全一样:
if(isset($_POST['test'])) {
$myvar = $_POST['test'];
}
else {
$myvar = 'N/A';
}
为什么安装XAMPP? – elboletaire 2012-04-17 17:59:52
你确定它是正确的脚本,它说第40行... – thumbmunkeys 2012-04-17 18:01:30
可能重复[PHP $ _GET和$ _POST未定义问题](http://stackoverflow.com/questions/1359240/php-get-and- post-undefined-problem) – 2012-04-17 18:16:05