我可以在 PHP 中为 $_POST 变量赋值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7433243/
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
Can I assign a value to a $_POST variable in PHP?
提问by KRB
For example, I am using a $_POST variable to insert data into a DB. Just before this query I have a few tests and if they are true I want to adjust that (hidden) $_POST value.
例如,我使用 $_POST 变量将数据插入到数据库中。就在这个查询之前,我有一些测试,如果它们是真的,我想调整那个(隐藏的)$_POST 值。
Ex.
前任。
if($baby_dragon_eats_children){
$_POST['hidden_value'] = "grapes";
}
Can $_POST['hidden_value'] be assigned a new value and then be passed to another function as $_POST and be able to access the new $_POST['hidden_value']?
是否可以为 $_POST['hidden_value'] 分配一个新值,然后将其作为 $_POST 传递给另一个函数并能够访问新的 $_POST['hidden_value']?
Thanks
谢谢
$_POST['consolidate_answers']
$_POST['consolidate_answers']
- IFyou assign a value to $_POST you should document is very clearly as it is not common nor considered "best" practice.
- IFyou have any extensions of PHP such as Suhosin Patch... it may block such actions..
- Handleyour own arrays, don't depend on $_POST!
- IFneed be, make a copy of $_POST and work with that.
- 如果您为 $_POST 分配一个值,您应该非常清楚地记录,因为它不常见,也不被认为是“最佳”实践。
- 如果您有任何 PHP 扩展,例如Suhosin Patch...它可能会阻止此类操作..
- 处理你自己的数组,不要依赖 $_POST!
- 如果需要,请复制 $_POST 并使用它。
回答by Michael Berkowski
You can assign values to $_POST
, but if you do so you should document it very clearly with comments both at the point of assignment and the later point of access. Manually manipulating $_POST
can break future programmers' (including your own) expectations of what is in the superglobal and where it came from.
您可以将值分配给$_POST
,但如果您这样做,您应该在分配点和稍后的访问点用注释非常清楚地记录它。手动操作$_POST
可能会打破未来程序员(包括您自己)对超全局中的内容及其来源的期望。
There may be other alternatives, like:
可能还有其他选择,例如:
$my_post = $_POST;
$my_post['hidden_value'] = 12345;
// In a function:
function func() {
// Emulate a superglobal
echo $GLOBALS['mypost']['hidden_value'];
}
回答by Pekka
Can $_POST['hidden_value'] be assigned a new value and then be passed to another function as $_POST and be able to access the new $_POST['hidden_value']?
是否可以为 $_POST['hidden_value'] 分配一个新值,然后将其作为 $_POST 传递给另一个函数并能够访问新的 $_POST['hidden_value']?
It can in normal PHP.
它可以在普通的 PHP 中。
However, extensions like the Suhosin Patchmay block this.
但是,像Suhosin Patch这样的扩展程序可能会阻止此操作。
Also, it feels wrong in general. $_POST
is intended to contain the raw, incoming POST data - nothing else. It should not be modified IMO.
此外,总体感觉是错误的。$_POST
旨在包含原始的传入 POST 数据 - 仅此而已。不应修改 IMO。
A better way to go would be to fetch all the data you plan to insert into the database into an array, and do the manipulations in that array instead.
更好的方法是将您计划插入数据库的所有数据提取到一个数组中,然后在该数组中进行操作。
回答by DaveRandom
You can, it will work, but don't.
你可以,它会工作,但不要。
Create a copy of $_POST
, perform your tests on that and modify that instead. For example.
创建 的副本$_POST
,对其执行测试并进行修改。例如。
$postdata = $_POST;
if ($postdata['somevalue'] != 'someothervalue') $postdata['newvalue'] = 'anothervalue';
// ...
If you manipulate variables auto-created by PHP, it will probably come back to haunt you later on when the variable doesn't hold the data you expect it to.
如果您操作由 PHP 自动创建的变量,那么当变量没有保存您期望的数据时,它可能会在稍后再次困扰您。
回答by Yam Marcovic
Find an alternative!
寻找替代品!
The $_POST variable has a distinct responsibility. Do not abuse its globalism.
$_POST 变量有不同的职责。不要滥用其全球主义。
I'd say don't abuse globalism either way, even if it means changing the design you have (or have in mind).
我想说无论如何都不要滥用全球化,即使这意味着改变你拥有(或想到的)设计。
But if you choose not to, then consider creating a dedicated global registry just for your needs. Don't manipulate language built-ins.
但是,如果您选择不这样做,则可以考虑创建一个专门满足您需求的全局注册表。不要操纵语言内置程序。