php 检查是否填充了 $_POST 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5923451/
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
Checking if $_POST variable is populated
提问by AKor
Possible Duplicate:
Check if $_POST exists
可能重复:
检查 $_POST 是否存在
I'm trying to run something if and only if a $_POST var is populated.
当且仅当填充了 $_POST var 时,我才尝试运行某些东西。
Can I do if(empty($_POST[...])) { ... }
? Or should I go about this another way?
我可以if(empty($_POST[...])) { ... }
吗?或者我应该以另一种方式解决这个问题?
回答by Rifat
I'd do if(isset($_POST['key'])) { ... }
我会做 if(isset($_POST['key'])) { ... }
回答by markus
No, empty() is not the right way of doing it. You have to use isset().
不,empty() 不是正确的做法。你必须使用isset()。
Why? Because many things are considered empty which you probably don't want to miss!
为什么?因为很多东西都被认为是空的,你可能不想错过!
The following things are considered to be empty:
以下内容被认为是空的:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
回答by GodsBoss
You can check $_SERVER['REQUEST_METHOD']
wether it is POST
or something else. See $_SERVER.
你可以检查$_SERVER['REQUEST_METHOD']
它是否是POST
或其他东西。请参阅$_SERVER。
Ooops, I totally misread your question. Do you want to test for a specificentry in $_POST
? Then use array_key_exists($key, $_POST)
.
哎呀,我完全误读了你的问题。您想测试 中的特定条目$_POST
吗?然后使用array_key_exists($key, $_POST)
.