doudi8519
2014-04-05 10:46
采纳率: 100%
浏览 102
已采纳

PHP正确检查post变量是否为空

What is the correct way to check if a $_POST['var'] variable is empty?

For example, say I have a page with a form that needs client side and server side validation, and if any of the validation fails then the errors are displayed on labels on the form. The same labels will be used to display the error messages from either failed client-side validation or failed server-side validation.

This means that in the label I would have to do something like this:

<?php
if (isset($_POST['var'])) {

    echo $_POST['var'];
}
else {

    echo 'Please ensure you have entered your details';
}

?>

However I have heard that it is wrong to check if a $_Post variable is set if theres a possibility it is empty?

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

检查$ _POST ['var']变量是否为空的正确方法是什么?

例如,假设我有一个页面,其表单需要客户端和服务器端验证,如果任何验证失败,则错误将显示在表单上的标签上。 相同的标签将用于显示来自客户端验证失败或服务器端验证失败的错误消息。

这意味着在标签中我将不得不做这样的事情 :

 &lt;?php 
if(isset($ _ POST ['var'])){
 
 echo $ _POST ['var']; 
}  
else {
 
 echo'请确保您输入了详细信息'; 
} 
 
?&gt; 
   
 
 

但是我听说过 检查$ _Post变量是否设置是否有空是错误的?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douluanji8752 2014-04-05 10:51
    已采纳

    This link may be useful: https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

    Use the empty function to check if a variable contents something:

    <?php
    if (isset($_POST['var']) && !empty($_POST['var'])) {
        echo $_POST['var'];
    }
    else {
        echo 'Please ensure you have entered your details';
    }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtc99987 2014-04-05 10:50

    First check with isset() to ensure that field is set. But isset() doesn't confirm a POST variable has non empty data. To ensure that use empty()

    <?PHP
    if(isset($_POST['var']) && !empty($_POST['var'])) {
        echo $_POST['var'];
    }
    ?>
    

    Note that always check isset() before checking empty otherwise its totally meaningless to check if a variable even exists or not after checking if it's empty or not.

    评论
    解决 无用
    打赏 举报
  • doulei6330 2014-04-05 10:53

    You can use:

    if(isset($_POST['var']) && !is_null($_POST['var']) && !empty($_POST['var']))
    {
    echo $_POST['var'];
    
    }
    
    评论
    解决 无用
    打赏 举报
  • drap5081683 2014-04-05 11:08

    Here's a possible solution.

    if (isset($_POST['var']))
    {
        if (!empty($_POST['var']))
        {
            echo $_POST['var'];
        }
        else
        {
            echo 'Please ensure you have entered your details';
        }
    }
    else
    {
        echo 'Please ensure you have entered your details';
    } 
    

    Why is !empty() alone not enough?

    It appears that empty() doesn't generate any warnings if the variable is not set. So it is necessary for you to check that the variable exists, at first. empty() seems to essentially equate to !isset($var) || $var == false as mentioned in the docs[1].

    [1] http://in1.php.net/empty

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题