php $ _GET和未定义的索引

Jef*_*eff 42 php undefined

当我尝试在不同的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>"
?>
Run Code Online (Sandbox Code Playgroud)

但现在,在我本地计算机上的本地服务器(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
Run Code Online (Sandbox Code Playgroud)

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

你能帮助我吗?

Rud*_*ser 67

错误报告将不包括先前服务器上的通知,这就是您没有看到错误的原因.

在尝试使用索引之前,您应该检查索引是否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>";
}
Run Code Online (Sandbox Code Playgroud)

使用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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:BTW,我写的第一组代码模仿了你的整体意图.意外值的预期结果是否?s=意味着不输出<body>标签或这是否是疏忽?请注意,交换机将通过始终默认来解决此问题<body>.

  • 比我更好的解决方案.为此+1 (2认同)

Pau*_*xon 6

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

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

您可以禁用通知报告,但处理它们是良好的卫生习惯,并且可以让您发现您可能错过的问题.


evi*_*nix 5

我总是使用实用函数/类来从$ _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');
Run Code Online (Sandbox Code Playgroud)