php 检查 $_POST 是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3496971/
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
Check if $_POST exists
提问by eliwedel
I'm trying to check whether a $_POST exists and if it does, print it inside another string, if not, don't print at all.
我正在尝试检查 $_POST 是否存在,如果存在,则将其打印在另一个字符串中,如果不存在,则根本不打印。
something like this:
像这样:
$fromPerson = '+from%3A'.$_POST['fromPerson'];
function fromPerson() {
if !($_POST['fromPerson']) {
print ''
} else {
print $fromPerson
};
}
$newString = fromPerson();
Any help would be great!
任何帮助都会很棒!
回答by ehmad
if( isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
回答by Dheeraj Bhaskar
Simple. You've two choices:
简单的。你有两个选择:
1. Check if there's ANY post data at all
1. 检查是否有任何帖子数据
//Note: This resolves as true even if all $_POST values are empty strings
if (!empty($_POST))
{
// handle post data
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
(OR)
(或者)
2. Only check if a PARTICULAR Key is available in post data
2. 仅检查发布数据中是否有 PARTICULAR Key 可用
if (isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
回答by Rafael
Everyone is saying to use isset() - which will probably work for you.
每个人都说要使用 isset() - 这可能对你有用。
However, it's important that you understand the difference between
但是,重要的是您要了解两者之间的区别
$_POST['x'] = NULL;
and $_POST['x'] = '';
$_POST['x'] = NULL;
和 $_POST['x'] = '';
isset($_POST['x'])
will return false
on the first example, but will return true
on the second one even though if you tried to print either one, both would return a blank value.
isset($_POST['x'])
将false
在第一个示例中返回,但会true
在第二个示例中返回,即使您尝试打印其中一个,两者都会返回一个空白值。
If your $_POST
is coming from a user-inputted field/form and is left blank, I BELIEVE (I am not 100% certain on this though) that the value will be "" but NOT NULL.
如果您$_POST
来自用户输入的字段/表单并且留空,我相信(尽管我不是 100% 确定)该值将是“”但不是 NULL。
Even if that assumption is incorrect (someone please correct me if I'm wrong!) the above is still good to know for future use.
即使这个假设是不正确的(如果我错了,请有人纠正我!)上面的内容对于将来使用仍然很有帮助。
回答by John Magnolia
Surprised it has not been mentioned
惊讶它没有被提及
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['fromPerson'])){
回答by h3xStream
isset($_POST['fromPerson'])
回答by Robert
The proper way of checking if array key exists is function array_key_exists()
检查数组键是否存在的正确方法是函数 array_key_exists()
The difference is that when you have $_POST['variable'] = null
it means that key exists and was send but value was null
不同之处在于,当您拥有$_POST['variable'] = null
它时意味着密钥存在并已发送但值为空
The other option is isset()
which which will check if array key exists and if it was set
另一个选项是isset()
哪个将检查数组键是否存在以及是否已设置
The last option is to use empty()
which will check if array key exists if is set and if value is not considered empty.
最后一个选项是使用empty()
which 将检查数组键是否存在,如果设置了并且值不被认为是空的。
Examples:
例子:
$arr = [
'a' => null,
'b' => '',
'c' => 1
];
array_key_exists('a', $arr); // true
isset($arr['a']); // false
empty($arr['a']); // true
array_key_exists('b', $arr); // true
isset($arr['b']); // true
empty($arr['b']); // true
array_key_exists('c', $arr); // true
isset($arr['c']); // true
empty($arr['c']); // false
Regarding your question
关于你的问题
The proper way to check if value was send is to use array_key_exists() with check of request method
检查值是否发送的正确方法是使用 array_key_exists() 检查请求方法
if ($_SERVER['REQUEST_METHOD'] == 'POST' && array_key_exists('fromPerson', $_POST)
{
// logic
}
But there are some cases depends on your logic where isset()
and empty()
can be good as well.
但是在某些情况下,取决于您的逻辑在哪里isset()
并且empty()
也可以很好。
回答by Bronek
- In that case using method
isset
is not appropriate.
- 在那种情况下使用方法
isset
是不合适的。
According to PHP documentation: http://php.net/manual/en/function.array-key-exists.php
(see Example #2 array_key_exists() vs isset())
The method array_key_exists
is intended for checking key presence in array.
根据 PHP 文档:http: //php.net/manual/en/function.array-key-exists.php
(参见示例 #2 array_key_exists() vs isset())
该方法array_key_exists
用于检查数组中的键是否存在。
So code in the question could be changed as follow:
所以问题中的代码可以更改如下:
function fromPerson() {
if (array_key_exists('fromPerson', $_POST) == FALSE) {
return '';
} else {
return '+from%3A'.$_POST['fromPerson'];
};
}
$newString = fromPerson();
- Checking presence of array $_POST is not necessary because it is PHP environment global variable since version 4.1.0 (nowadays we does not meet older versions of PHP).
- 没有必要检查数组 $_POST 是否存在,因为它是自 4.1.0 版以来的 PHP 环境全局变量(现在我们不满足旧版本的 PHP)。
回答by linuxatico
All the methods are actually discouraged, it's a warning in Netbeans 7.4 and it surely is a good practice not to access superglobal variables directly, use a filterinstead
实际上不鼓励使用所有方法,这是 Netbeans 7.4 中的警告,不直接访问超全局变量肯定是一个好习惯,而是使用过滤器
$fromPerson = filter_input(INPUT_POST, 'fromPerson', FILTER_DEFAULT);
if($fromPerson === NULL) { /*$fromPerson is not present*/ }
else{ /*present*/ }
var_dump($fromPerson);exit(0);
回答by Augustus Francis
Try
尝试
if (isset($_POST['fromPerson']) && $_POST['fromPerson'] != "") {
echo "Cool";
}