PHP:函数中的 $_GET 和 $_POST?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354691/
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
PHP: $_GET and $_POST in functions?
提问by hhh
I am flabbergasted by the code, where the GET-values, such as $_GET['username'], are not included as parameters to functions.
我对代码大吃一惊,其中的 GET 值(例如 )$_GET['username']没有作为函数的参数包含在内。
When do you need to include POST and GET methods as parameters to functions?
什么时候需要包含 POST 和 GET 方法作为函数的参数?
回答by Pascal MARTIN
When do you you need to include POST and GET methods as parameters to functions?
什么时候需要将 POST 和 GET 方法作为函数的参数包含在内?
I would say "never": $_GETand $_POSTare what is called superglobals: they exists in the whole script; which means they exist inside functions/methods.
我会说“从不”:$_GET并且$_POST是所谓的超全局变量:它们存在于整个脚本中;这意味着它们存在于函数/方法中。
Especially, you don't need to you the globalkeyword for those.
特别是,您不需要global为这些关键字提供关键字。
Still, relying on those in your functions/methods is quite a bad practice: your functions/methods should generally not depend on anything not passed as a parameter.
尽管如此,依赖于您的函数/方法中的那些是一种非常糟糕的做法:您的函数/方法通常不应依赖于任何未作为参数传递的内容。
What I mean is; consider those two functions:
我的意思是; 考虑这两个函数:
function check_login_password()
{
$login = $_GET['login'];
$password = $_GET['password'];
// Work with $login and $password
}
and
和
/**
* Check login and password
*
* @param $login string
* @param $password string
* @return boolean
*/
function check_login_password($login, $password)
{
// Work with $login and $password
}
OK, with the first one, you don't have to pass two parameters... But that function will not be independent and will not work in any situation where you'd have to check a couple of login/password that doesn't come from $_GET.
好的,对于第一个,您不必传递两个参数......但是该函数不会是独立的,并且在您必须检查几个不存在的登录名/密码的任何情况下都不起作用来自$_GET.
With the second function, the caller is responsible for passing the right parameters; which mean they can come from wherever you want: the function will always be able to do its job.
使用第二个函数,调用者负责传递正确的参数;这意味着它们可以来自您想要的任何地方:该功能将始终能够完成其工作。
回答by OIS
$_GET and $_POST are global variables. They have values, not methods.
$_GET 和 $_POST 是全局变量。他们有价值观,而不是方法。
It is their values you want to send to functions, and classes/functions should generally be unaware of anything outside them, like where the data comes from. Many use the shortcut of using the global variables in functions, and thus limiting the usability of their functions.
你想要发送给函数的是它们的值,类/函数通常应该不知道它们之外的任何东西,比如数据来自哪里。许多使用在函数中使用全局变量的快捷方式,从而限制了其函数的可用性。
回答by rogeriopvl
You don't need to send them as function parameters as they are global variables (accessible everywhere in the code).
您不需要将它们作为函数参数发送,因为它们是全局变量(可在代码中的任何地方访问)。
But it's always a good practice to filter and validate them before you use them in your code.
但是在代码中使用它们之前过滤和验证它们总是一个好习惯。

