PHP $ _GET和未定义的索引

我试图在不同的PHP服务器上运行我的脚本时,出现了一个新问题。

在我的旧服务器上,下面的代码显示工作正常 – 即使没有声明s参数。

 <?php if ($_GET['s'] == 'jwshxnsyllabus') echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">"; if ($_GET['s'] == 'aquinas') echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">"; if ($_GET['s'] == 'POP2') echo "<body onload=\"loadSyllabi('POP2')\">"; elseif ($_GET['s'] == null) echo "<body>" ?> 

但是现在,在本地机器上的本地服务器(XAMPP-Apache)上,当没有定义s值时,出现以下错误。

 Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 43 Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 45 Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 47 Notice: Undefined index: s in C:\xampp\htdocs\teaching\index.php on line 49 

我想为脚本调用某些JavaScript函数,如果为s声明了一个值,但是如果没有声明,我希望页面正常加载。

你可以帮我吗?

错误报告将不包括以前的服务器上的通知,这就是为什么你没有看到错误。

在尝试使用它之前,您应该检查索引是否实际存在于$_GET数组中。

像这样的东西就足够了:

 if (isset($_GET['s'])) { if ($_GET['s'] == 'jwshxnsyllabus') echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">"; else if ($_GET['s'] == 'aquinas') echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">"; else if ($_GET['s'] == 'POP2') echo "<body onload=\"loadSyllabi('POP2')\">"; } else { echo "<body>"; } 

这可能是有益的(如果你打算增加更多的情况下)使用switch语句使你的代码更具可读性。

 switch ((isset($_GET['s']) ? $_GET['s'] : '')) { case 'jwshxnsyllabus': echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">"; break; case 'aquinas': echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">"; break; case 'POP2': echo "<body onload=\"loadSyllabi('POP2')\">"; break; default: echo "<body>"; break; } 

编辑:顺便说一句,我写的第一套代码模仿你的意思是完整的。 意料之中的预期结果是?s=意味着不输出<body>标签,或者这是一个疏忽? 请注意,交换机将通过总是默认为<body>来解决这个问题。

养成检查variables是否可用于isset的习惯,例如

 if (isset($_GET['s'])) { //do stuff that requires 's' } else { //do stuff that doesn't need 's' } 

你可以禁用通知报告,但是处理这些报告很卫生,可以让你发现你可能会错过的问题。

我总是使用实用程序函数/类从$ _GET和$ _POST数组中读取,以避免总是检查索引是否存在…像这样的事情会做的。

 class Input { function get($name) { return isset($_GET[$name]) ? $_GET[$name] : null; } function post($name) { return isset($_POST[$name]) ? $_POST[$name] : null; } function get_post($name) { return $this->get($name) ? $this->get($name) : $this->post($name); } } $input = new Input; $page = $input->get_post('page'); 

我在使用xampp的localhost中遇到同样的问题。 现在我正在使用这个参数组合:

 // Report all errors except E_NOTICE // This is the default value set in php.ini error_reporting(E_ALL ^ E_NOTICE); 

php.net:http://php.net/manual/pt_BR/function.error-reporting.php

首先检查$_GET['s']是否设置。 像这样改变你的条件

 <?php if (isset($_GET['s']) && $_GET['s'] == 'jwshxnsyllabus') echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')\">"; elseif (isset($_GET['s']) && $_GET['s'] == 'aquinas') echo "<body onload=\"loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')\">"; elseif (isset($_GET['s']) && $_GET['s'] == 'POP2') echo "<body onload=\"loadSyllabi('POP2')\">"; elseif (isset($_GET['s']) && $_GET['s'] == null) echo "<body>" ?> 

而且还妥善处理你的条件

事实上,没有一个build议的答案,即使是一个好的做法,也会消除这个警告。

为了正确,我会做以下几点:

 function getParameter($param, $defaultValue) { if (array_key_exists($param, $_GET)) { $value=$_GET[$param]; return isSet($value)?$value:$defaultValue; } return $defaultValue; } 

这样,我检查_GET数组的键存在没有触发警告。 禁用警告并不是一个好主意,因为很多时候他们至less有兴趣去看看。

要使用你刚刚做的function:

 $myvar = getParameter("getparamer", "defaultValue") 

所以如果参数存在,你会得到这个值,如果没有,你会得到defaultValue。

在使用之前,你应该检查索引是否存在(比较它)

 if (isset($_GET['s']) AND $_GET['s'] == 'foobar') { echo "foo"; } 

使用E_ALL | E_STRICT开发中!

避免if,else和elseif!

 $loadMethod = ""; if(isset($_GET['s'])){ switch($_GET['s']){ case 'jwshxnsyllabus': $loadMethod = "loadSyllabi('syllabus', '../syllabi/jwshxnporsyllabus.xml', '../bibliographies/jwshxnbibliography_')"; break; case 'aquinas': $loadMethod = "loadSyllabi('syllabus', '../syllabi/AquinasSyllabus.xml')"; break; case 'POP2': $loadMethod = "loadSyllabi('POP2')"; } } echo '<body onload="'.$loadMethod.'">'; 

干净,可读的代码是可维护的代码

我build议你在盲目访问之前检查你的数组:

 if(isset($_GET['s'])){ if ($_GET['s'] == 'jwshxnsyllabus') /* your code here*/ } 

另一种(快速)修复方法是通过在脚本的顶部写入来禁用错误报告:

 error_reporting(0); 

在你的情况下,很可能你的其他服务器的php.ini的错误报告configuration被设置为0。
通过以0作为参数调用error_reporting ,您将closures所有通知/警告和错误。 有关更多详细信息,请查看php手册 。

请记住,这是一个快速解决scheme,强烈build议避免错误,而不是忽略它们。