php 如何检测是否设置了 $_POST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15220704/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:49:06  来源:igfitidea点击:

How to detect if $_POST is set?

phppost

提问by Lisa Miskovsky

I want to know how to detect if $_POST is set or not.

我想知道如何检测是否设置了 $_POST。

Right now I detect it like this:

现在我检测它是这样的:

if(isset($_POST['value']))

But I'm not looking if value is set anymore. Basically, any POST will work.

但我不再关注是否设置了价值。基本上,任何 POST 都可以。

if(isset($_POST))

I'm not sure how PHP handle this. Perhabs isset($_POST) is always returns true since it's a PHP global?

我不确定 PHP 如何处理这个问题。Perhabs isset($_POST) 总是返回 true,因为它是 PHP 全局的?

Basically, how can I do this?

基本上,我该怎么做?

回答by hsz

Try with:

尝试:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {}

to check if your script was POSTed.

检查您的脚本是否已发布。

If additional data was passed, $_POSTwill not be empty, otherwise it will.

如果传递了附加数据,$_POST则不会为空,否则为空。

You can use emptymethod to check if it contains data.

您可以使用empty方法来检查它是否包含数据。

if ( !empty($_POST) ) {}

回答by Salman A

$_POSTis an array. You can check:

$_POST是一个数组。您可以检查:

count($_POST)

If it is greater than zero that means some values were posted.

如果它大于零,则意味着发布了一些值。

回答by niaccurshi

A simple solution may well be to just use

一个简单的解决方案很可能只是使用

if (!empty($_POST))

回答by Dipesh Parmar

Just use it as below. because its super globalso its always return true when checking for issetand empty.

只需如下使用它。因为它super global在检查issetand时总是返回 true empty

<?php
    if($_POST)
    {
        echo "yes";
    }
?>

回答by Spencer Mark

I know this answer has already been answered, but here's a simple method I'm using in one of my classes to figure whether the post has been set (perhaps someone will find it useful):

我知道已经回答了这个答案,但这是我在我的一个课程中使用的一种简单方法来确定帖子是否已设置(也许有人会发现它很有用):

public function isPost($key = null) {

    if ($_SERVER['REQUEST_METHOD'] != 'POST') {

        return false;

    }

    if (!empty($key)) {

        return isset($_POST[$key]);

    }

    return true;

}

回答by Hasan bd

Best way to check $_POST

检查 $_POST 的最佳方法

<?php 
if(isset($_POST['value']) && !empty($_POST['value']))
{
return true;
}
else
{
return false;
}