未定义的索引:php 脚本中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2160368/
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 05:17:07  来源:igfitidea点击:

Undefined index: Error in php script

phprequest

提问by Yogi Yang 007

In a php page I have following code:

在 php 页面中,我有以下代码:

if($_REQUEST['c']!="") // I get error on this line itself. Why?
{
 $pidis=(int)($_REQUEST['c']);
}

I keep getting Undefined index error.

我不断收到未定义的索引错误。

On Googling I manage to understand that if a page is access without parameters (in URL) which we are trying to access we can get this error/warning. I believe that if a parameter is not defined in the URL it should just return empty instead of giving error/warning message.

在谷歌搜索上,我设法理解,如果我们尝试访问的页面没有参数(在 URL 中),我们可能会收到此错误/警告。我相信如果一个参数没有在 URL 中定义,它应该只返回空而不是给出错误/警告消息。

I know that it is possible to suppress errors and warning by adding

我知道可以通过添加来抑制错误和警告

error_reporting(E_ALL ^ E_NOTICE);

error_reporting(E_ALL ^ E_NOTICE);

But I do not want to do this.

但我不想这样做。

This same page work just fine on our company's web server but does not work on our clients web server.

同样的页面在我们公司的网络服务器上工作得很好,但在我们的客户网络服务器上不起作用。

Why is this happening?

为什么会这样?

How to solve this problem?

如何解决这个问题呢?

回答by zombat

You are getting that error because you are attempting to compare $_REQUEST['c']to something when $_REQUEST['c']does not exist.

您收到该错误是因为您试图与不存在的$_REQUEST['c']东西进行比较$_REQUEST['c']

The solution is to use isset()before comparing it. This will remove the warning, since the comparison won't happen if $_REQUEST['c']doesn't exist.

解决方案是在比较之前使用isset()。这将删除警告,因为如果$_REQUEST['c']不存在,则不会进行比较。

if(isset($_REQUEST['c']) && $_REQUEST['c']!="")
{
 $pidis=(int)($_REQUEST['c']);
}

It is an E_NOTICElevel error, and your level of error reportingwill affect whether the error shows up or not. Your client's server has E_NOTICElevel error reporting turned on, which is why it shows up there.

这是一个E_NOTICE级别错误,您的错误报告级别将影响错误是否出现。您客户端的服务器E_NOTICE打开了级别错误报告,这就是它出现在那里的原因。

It is a good idea to always develop using E_ALLso that you can catch this kind of error before moving your code to other servers.

始终使用开发是一个好主意,E_ALL以便您可以在将代码移动到其他服务器之前捕获此类错误。

回答by Bryan

Another solution is to use the following:

另一种解决方案是使用以下内容:

$pidis = isset($_REQUEST['c']) ? $_REQUEST['c'] : '';

You can also, if you prefer to return a value other than empty, by placing a default value within the final set of single quotes, e.g.

如果您更喜欢返回空值以外的值,您也可以通过在最后一组单引号中放置一个默认值,例如

$pidis = isset($_REQUEST['c']) ? $_REQUEST['c'] : 'Default Value';

or return a different variable type, for instance an integer:

或返回不同的变量类型,例如整数:

$pidis = isset($_REQUEST['c']) ? $_REQUEST['c'] : 34;

回答by Felix Kling

Instead of isset()you can also use: array_key_exists().

相反的isset(),你也可以使用:array_key_exists()

The difference between both methods is that isset()checks also whether the value of the variable is null. If it is nullthen issetreturns falsewhereas array_key_exists()returns always trueif the key exists (no mater which value). E.g.:

两种方法的区别在于,isset()还要检查变量的值是否为null。如果是,nullisset返回,false而如果键存在,则array_key_exists()始终返回true(无论是哪个值)。例如:

$array = array('c' => null);

var_dump(isset($array['c']))); // isset() returns FALSE here
var_dump(array_key_exists($array['c']); //array_key_exists() returns TRUE

Depending on the context, it is important to distinguish this. In your case I don't think it matters doesn't matter, as (I guess) a request parameter never will be null(except one overwrites it manually).

根据上下文,区分这一点很重要。在您的情况下,我认为这无关紧要,因为(我猜)请求参数永远不会null(除非手动覆盖它)。

回答by Ignacio Vazquez-Abrams

Use isset($_REQUEST['c'])to test if it exists first.

用于isset($_REQUEST['c'])先测试它是否存在。

回答by Pascal MARTIN

PHP is giving a notice (which is not an error : it's just a notice)when you are trying to use a variable that doesn't exists, or an array element that doesn't exist.

当您尝试使用不存在的变量或不存在的数组元素时,PHP 会发出通知(这不是错误:这只是一个通知)

This is just to help you, and you should not mask those notices : they are here to help you -- for instance, to help you detect typos in variable names.

这只是为了帮助您,您不应掩盖这些注意事项:它们是来帮助您的——例如,帮助您检测变量名称中的拼写错误。

Before using that array index, if it's not always present, you should test if it's here, using isset:

在使用该数组索引之前,如果它不总是存在,您应该测试它是否在这里,使用isset

if (isset($_REQUEST['c']) && $_REQUEST['c']!="") {
    // ...
}