php 如何解决未定义索引错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18888464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:24:41  来源:igfitidea点击:

How to solve undefined index error

phpundefined-index

提问by Alin

if(isset($_SESSION['evt_year']) 
   || isset($_SESSION['evt_title']) 
   || isset($_SESSION['evt_sdate']) 
   || isset($_SESSION['evt_place']) 
   || isset($_SESSION['evt_stime']) 
   || isset($_SESSION['evt_etime']) 
   || isset($_SESSION['evt_desc'])) {
    $output.=$_GET['title']; //the error is here
}
else {
    $output.="";
}

Notice the error I got:

注意我得到的错误:

Undefined index: title in C:\xampp\htdocs\ICT\abc\cal1\EventCalender\classes\EventRecordForm.php on line 13

未定义的索引:标题在 C:\xampp\htdocs\ICT\abc\cal1\EventCalender\classes\EventRecordForm.php 中的第 13 行

回答by AD7six

You are testing a lot of variables, but none of them are the variable that is read from.

您正在测试很多变量,但没有一个是从中读取的变量。

should be e.g.:

应该是例如:

if (isset($_GET['title'])) {
     $output.=$_GET['title']; // there is no error here
}

回答by Patrick Moore

Just check isset()on the $_GETvariable before appending it:

只是检查isset()$_GET变量追加之前:

if ( isset( $_GET['title'] ) ) $output.=$_GET['title'];

if ( isset( $_GET['title'] ) ) $output.=$_GET['title'];

The error occurs because $_GET['title'] has not been populated.

发生错误是因为 $_GET['title'] 尚未填充。

回答by Sliq

Before you check/use a variable, it needs to be defined or checked it it really exists. This has been introduced in PHP 5.3.0.

在检查/使用变量之前,需要定义它或检查它是否确实存在。这已在 PHP 5.3.0 中引入。

WRONG:

错误的:

$output = $_GET['title'];

CORRECT:

正确的:

if (isset($_GET['title'])) {
    $output = $_GET['title'];
}