PHP $ _GET和未定义的索引 [英] php $_GET and undefined index

查看:164
本文介绍了PHP $ _GET和未定义的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在其他PHP服务器上运行脚本时,出现了一个新问题.

A new problem has arisen for me as I tried to run my script on a different PHP Server.

在旧服务器上,即使未声明s参数,以下代码也可以正常工作.

ON my old server the following code appears to work fine - even when no s parameter is declared.

<?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>"
?>

但是现在,在未定义s的值的情况下,在本地计算机(XAMPP-Apache)上的本地服务器上,出现以下错误.

But now, on a my local server on my local machine (XAMPP - Apache) I get the following error when no value for s is defined.

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

如果为s声明了值,但脚本要调用某些javascript函数会发生什么,但是如果未声明任何内容,我希望页面能够正常加载.

What I want to happen for the script to call certain javascript functions if a value is declared for s, but if nothing is declared i would like the page to load normally.

你能帮我吗?

推荐答案

错误报告不会在以前的服务器上包含通知,这就是为什么您没有看到错误的原因.

Error reporting will have not included notices on the previous server which is why you haven't seen the errors.

在尝试使用索引s之前,应该检查索引s是否确实存在于数组$_GET中.

You should be checking whether the index s actually exists in the $_GET array before attempting to use it.

像这样的东西就足够了:

Something like this would be suffice:

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语句来提高代码的可读性(如果打算增加更多的案例)可能是有益的.

It may be beneficial (if you plan on adding more cases) to use a switch statement to make your code more readable.

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>来解决此问题.

BTW, the first set of code I wrote mimics what yours is meant to do in it's entirety. Is the expected outcome of an unexpected value in ?s= meant to output no <body> tag or was this an oversight? Note that the switch will fix this by always defaulting to <body>.

这篇关于PHP $ _GET和未定义的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆