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 Rel = “ noReferrer”> filter < a href = “ http://www.php.net/Manual/en/book.filter.php”rel = “ noReferrer”> filter instead
$fromPerson = filter_input(INPUT_POST, 'fromPerson', FILTER_DEFAULT);
if($fromPerson === NULL) { /*$fromPerson is not present*/ }
else{ /*present*/ }
var_dump($fromPerson);exit(0);
//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;
}
(或)
2. 只检查邮件数据中是否有特定密钥
if (isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
I would like to add my answer even though this thread is years old and it ranked high in Google for me.
我最好的方法就是尝试:
if(sizeof($_POST) !== 0){
// Code...
}
As $_POST is an array, if the script loads and no data is present in the $_POST variable it will have an array length of 0. This can be used in an IF statement.
您可能还想知道这是否会抛出一个“未定义索引”错误,就好像我们正在检查是否设置了 $_POST... 实际上 $_POST总是存在,只有当您尝试搜索不存在的 $_ POST 数组值时,才会出现“未定义索引”错误。
class ParameterFetcher
{
public function fetchDate(string $pDate):string{
$myVar = "";
try{
if(strlen($_POST[$pDate]) > 0){
$myVar = $_POST[$pDate];
}
}catch (Exception $faild){
die("field NULL or not set for $pDate");
}
[ ... other stuff ]