【发布时间】:2011-09-04 10:08:03
【问题描述】:
可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
我刚刚学习 PHP,但我不断收到未定义索引错误。我正在学习的这本书有一个 HTML 表单和一个处理表单的 PHP 页面,使用以下格式:
<!-- The form fields are all set up something like this -->
<input type="text" id="howlong" name="howlong" /><br />
// The PHP starts with one line like this for each of the form fields in the HTML
$how_long = $_POST ['howlong'];
// And there is one line for each one like this to output the form data:
echo ' and were gone for ' . $how_long . '<br />';
我正在使用的示例有大约 12 个表单域。
奇怪的是,并非所有变量都会引发此错误,但我看不到它的规律。
我检查了所有 HTML 字段名是否与我输入的 PHP $_POST 变量名匹配,并且我确定当我填写并提交表单时,所有字段都填写了一些内容。有趣的是,可以为本书下载的完整代码也抛出了这个错误。
我意识到这段代码可能没有反映最佳实践,它来自本书的第一章,显然我是一个菜鸟 :)
以防万一,我在 XAMPP 1.7.4 和 Windows 7 Home Premium 上使用 PHP 5.3.5。
【问题讨论】: