PHP: $_GET and $_POST in functions?(PHP:函数中的 $_GET 和 $_POST?)
问题描述
我对代码大吃一惊,其中的 GET 值(例如 $_GET['username']
)并未作为函数的参数包含在内.
I am flabbergasted by the code, where the GET-values, such as $_GET['username']
, are not included as parameters to functions.
什么时候需要包含 POST 和 GET 方法作为函数的参数?
When do you need to include POST and GET methods as parameters to functions?
推荐答案
什么时候需要包含 POST和 GET 方法作为参数功能?
When do you you need to include POST and GET methods as parameters to functions?
我会说从不":$_GET
和 $_POST
就是所谓的 superglobals:它们存在于整个脚本中;这意味着它们存在于函数/方法中.
I would say "never": $_GET
and $_POST
are what is called superglobals: they exists in the whole script; which means they exist inside functions/methods.
特别是,您不需要 global
关键字.
Especially, you don't need to you the global
keyword for those.
尽管如此,依赖于您的函数/方法中的那些是一种非常糟糕的做法:您的函数/方法通常不应依赖于任何未作为参数传递的内容.
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
}
和
/**
* Check login and password
*
* @param $login string
* @param $password string
* @return boolean
*/
function check_login_password($login, $password)
{
// Work with $login and $password
}
好的,对于第一个,您不必传递两个参数...但是该函数不会是独立的,并且在您必须检查几个登录名/密码的任何情况下都不起作用不是来自 $_GET
.
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
.
使用第二个函数,调用者负责传递正确的参数;这意味着它们可以来自您想要的任何地方:该功能将始终能够完成其工作.
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.
这篇关于PHP:函数中的 $_GET 和 $_POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP:函数中的 $_GET 和 $_POST?
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01