php 使用 isset($_REQUEST["p"]) 或 $_REQUEST["p"]

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

Using isset($_REQUEST["p"]) or $_REQUEST["p"]

php

提问by calexandru

Sometime ago, I was in a internship, and I was working as a junior web-developer. While working and learning, I noticed that when changing pages, instead of using isset($_POST/GET/REQUEST["var"])they just used $_POST/GET/REQUEST["var"].

前段时间,我在实习,当时我是一名初级 Web 开发人员。在工作和学习时,我注意到在更改页面时,isset($_POST/GET/REQUEST["var"])他们只是使用$_POST/GET/REQUEST["var"].

So, later I came home, and tried the same thing. What happens ? Every-time I come across a if()to verify that, I have to use isset(), otherwhise it gives me an error. But notice one thing, my url is this:

所以,后来我回到家,并尝试了同样的事情。发生什么了 ?每次我遇到 aif()来验证时,我都必须使用isset(),否则它会给我一个错误。但请注意一件事,我的网址是这样的:

?p=sub_artigo&id=2

So, when I do the if()condition:

所以,当我做if()条件时:

if(isset($_REQUEST["p"])=="procurar" && $_REQUEST['cont']){

It doesn't show errors, but if I take of the isset(), it gives the usual error that I see in the forums and here.

它不显示错误,但如果我选择isset(),它会给出我在论坛和此处看到的常见错误。

So my question is, why doesn't show the error for the second variable ?

所以我的问题是,为什么不显示第二个变量的错误?

Note: p->string;id->int

注意:p->string;id->int

回答by castis

They have error_reportingturned down, which is nice because it means you can do things like

他们error_reporting拒绝了,这很好,因为这意味着你可以做这样的事情

if ($_POST['whatever']) { ... }

instead of

代替

if (isset($_POST['whatever'])) { ... }

but it also stops you from seeing other possibly pertinent errors.

但它也会阻止您看到其他可能相关的错误。

this setting is found in the php.inifile under the variable error_reporting.

此设置位于php.ini变量下的文件中error_reporting

More information on the ini file can be found here: http://php.net/manual/en/ini.php

可以在此处找到有关 ini 文件的更多信息:http: //php.net/manual/en/ini.php

also, isset($_REQUEST["p"])=="procurar"while sytactically correct, is never going to return true, because isset()returns a boolean value.

此外,isset($_REQUEST["p"])=="procurar"虽然在语法上是正确的,但永远不会返回 true,因为isset()返回的是一个布尔值。

what you want is isset($_REQUEST['p']) && $_REQUEST['p'] == 'procurar'

你想要的是 isset($_REQUEST['p']) && $_REQUEST['p'] == 'procurar'

回答by Marc B

RTM: http://php.net/isset

RTM:http: //php.net/isset

isset() returns a boolean TRUE/FALSE. It will NEVERreturn a string, so this statement

isset() 返回布尔值 TRUE/FALSE。它永远不会返回一个字符串,所以这个语句

if(isset($_REQUEST["p"])=="procurar" && $_REQUEST['cont']){ 

can NEVER succeed, because isset() will never EVER be equal to procurar, so the ['cont']check will never be evaluated.

永远不会成功,因为 isset() 永远不会等于procurar,因此['cont']永远不会评估检查。

回答by IT-Smart

When the first statement is false, PHP does not bother triyng the rest of the if statement.

当第一条语句为假时,PHP 不会费心去尝试 if 语句的其余部分。

I always use the following check on every $_REQUEST, $_POST or $_GET key:

我总是对每个 $_REQUEST、$_POST 或 $_GET 键使用以下检查:

function ifSet($key) {   
    return (isset($_REQUEST[$key]) && $_REQUEST[$key])?$_REQUEST[$key]:'';
}

This will never give you any warnings, even if error_reporting is set to E_ALL.

这永远不会给你任何警告,即使 error_reporting 设置为 E_ALL。

The 'isset' checks if the $key is set and after that it checks if $key has a value.

'isset' 检查是否设置了 $key,然后检查 $key 是否有值。