dtpk04526211
2012-12-26 09:51
浏览 368
已采纳

isset($ _ POST)是否正确使用?

The problem I'm facing is quite common I suppose but I didn't see a single thread in internet.

If I use isset($_POST),

  1. Will it always return true?
  2. Does the response depend on the version of PHP I use?
  3. Is $_POST is a variable? (it's a super global 'variable' after all). Because in php.net documentation, it is mentioned

isset() only works with variables as passing anything else will result in a parse error.

图片转代码服务由CSDN问答提供 功能建议

我想面对的问题很常见,但我没有看到互联网上的单个帖子。 / p>

如果我使用 isset($ _ POST)

  1. 它是否总是返回 true
  2. 响应是否取决于我使用的PHP版本?
  3. $ _ POST 是变量吗? (毕竟,这是一个超级全球'变量'。 因为在php.net文档中,提到

    isset()仅适用于变量,因为传递任何其他内容都会导致 解析错误。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • duaner5714 2012-12-26 09:55
    已采纳

    Will it always return true?

    Yes, even if the page was opened using GET method or nothing was POSTed.

    Does the response depend on the version of PHP I use?

    No it does not (not sure about very old versions of PHP).

    Is $_POST is a variable?

    Yes

    isset() only works with variables as passing anything else will result in a parse error.

    This is explicitly mentioned in the manual so that people do not try to do cheaky stuff. These won't work for example:

    function getVarName() { return '_POST'; }
    isset(getVarName());
    isset('$_POST');
    

    Now, why would you want to check if $_POST is set. Perhaps want to check if a certain variable (e.g. email) was posted, in that case you need to check:

    isset($_POST["email"])
    
    打赏 评论
  • doujionggan9570 2012-12-26 09:55

    isset($_POST); will always return true. If you want to check if it contains something use empty($_POST);

    Does the response depend on the version of PHP I use?

    No

    Is $_POST is a variable ?

    Yes

    打赏 评论
  • doutu9810 2012-12-26 09:56

    I would recommend using one of the following:

    if($_POST){
    
    }
    if(!empty($_POST)){
    
    }
    

    1, Will it always return true?

    isset($_POST) will always return true. If $_POST is empty it will return false.

    2, Does the response depend on the version of PHP I use?

    I'm not sure about versions below 4.* but the response has always been the same.

    3, Is $_POST is a variable?

    Yes, it is considered a superglobal like: $GLOBALS, $_SESSION, $_POST, $_GET

    打赏 评论
  • dourang8305 2012-12-26 09:56

    Whether or not you posted any data $_POST will always be set as an array. What you are probably looking for is the empty() method to see if any data was actually posted, like:

    if(!empty($_POST)) {
        // POST data was set
    }
    
    打赏 评论
  • doufubian3479 2012-12-26 09:59
    • Will it always return true?

    Yes it would always be true

    • Does the response depend on the version of PHP I use?

    No tested on PHP 4.3.0 - 5.4.10

    • Is $_POST is a variable?

    Definitely Yes

    Better way to validate $_POST is to use empty

    打赏 评论
  • dprc88435 2012-12-26 10:00
    Isset()
    

    return true only if it contains some value (it can be Zero 0). If it doesn't have any value then it returns false. If you want to prevent from (0) use

    if(isset($_POST) && $_POST) 
    

    This will be true if only it has non-zero value

    $_POST is global array
    
    Response doesn't depend upon the version of PHP
    
    打赏 评论

相关推荐 更多相似问题