避免在 PHP 中访问 $_POST、$_GET 和其他变量时使用 isset? [英] Avoid using isset in PHP when accessing $_POST, $_GET, and other variables?

查看:21
本文介绍了避免在 PHP 中访问 $_POST、$_GET 和其他变量时使用 isset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止 PHP 在每次尝试检查变量时返回 未定义变量 错误,如果它有内容并且某个变量尚未使用?在我之前的设置中,即使我还没有放入任何东西,我也可以检查 $_POST['email'] .它只是返回一个空白或空的结果.这就是我希望我的 PHP 设置能够工作的方式,但对于我的生活,我似乎无法弄清楚如何配置它.:(

how can I prevent PHP from returning an Undefined variable error every time I try to check a variable if it has contents and that certain variable hasn't been used yet? In my previous setup, I can check $_POST['email'] even if I haven't put anything into it yet. It simply returns a blank or empty result. That's how I want my PHP setup to work but for the life of me, I can't seem to find figure out how to configure it. :(

示例:

<?php
if ($_POST['info'] != '') {
  // do some stuff
}
?>
<form method="post">
<input type="text" name="info" />
<input type="submit" />
</form>

当您在单个 PHP 页面中使用上述脚本并在我当前的设置上运行它时,它会返回一个未定义变量错误.在我之前的设置中,该脚本工作得非常好.任何人都可以对我的这个问题分享一些看法.如果您需要更多详细信息,请直接说出来,我会尝试在此帖子中添加更多详细信息.

When you use the above script in a single PHP page and run it on my current setup, it returns an Undefined variable error. On my previous setup, that script worked like a charm. Can anyone share some light into this problem of mine. If you need further details, just say so and I will try to add more details to this post.

知道 isset() 检查,但我不想在我的所有脚本中使用它.我希望它不要对变量那么限制.

I know about the isset() checking but I don't want to use it on all of my scripts. I want it to not be that restrictive about variables.

推荐答案

很可能不是错误,而是警告.如果需要,您可以将 PHP 设置为不显示错误/警告.只需将其添加到代码的开头即可:

Most likely it's not an error, but a Warning. You can set PHP not to display errors/warnings if you want. Just add this to the begining of your code:

ini_set('display_errors', 0);

尽管如此,我还是建议您不要偷懒,不要让您的代码显示任何错误、警告、通知,安全总比抱歉好.

Altough, I recommend you not to be lazy and have your code not display any errors, warnings, notices whatsoever, better safe than sorry.

做你想做的事情的首选方式是

And the preferred way to do what you want to do is

<?php
if (isset($_POST['info'])) {
  // do some stuff
}
?>
<form method="post">
<input type="text" name="info" />
<input type="submit" />
</form>

这篇关于避免在 PHP 中访问 $_POST、$_GET 和其他变量时使用 isset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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