Isset 基于多个值

Isset on multiple values

如果同时设置,我想检查多个值。我从 isset 文档中读到 If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

那么为什么我的代码总是打印发送的值而从不打印 'no'?

PHP

if ( isset( $_POST['thread'],
        $_POST['winkey'],
        $_POST['dkey'],
        $_POST['winrew'],
        $_POST['drew'] )===true ) {
    echo $_POST['thread'];
    echo $_POST['winkey'];
    echo $_POST['dkey'];
    echo $_POST['winrew'];
    echo $_POST['drew'];
}
else echo 'no';

HTML

<form action="class.php" method="POST">
thread link:<br>
<input type="text" name="thread" >
<br>
win key:<br>
<input type="text" name="winkey" >
<br>
double key:<br>
<input type="text" name="dkey" >
<br>
winrew:<br>
<input type="text" name="winrew" >
<br>
double rew:<br>
<input type="text" name="drew" >
<br>
<input type="checkbox" name="banlist" value="ban">Include banlist<br>
<br>
<input type="submit" value="Submit">
</form> 

可能是因为当您 POST 表单时,即使您没有在输入字段中输入任何内容,也会设置值。对于这些键,您将得到一个空字符串(return TRUE isset())。

您可能应该使用 !empty() 进行检查。

更好的方法可能是使用 filter_input_array()。可能看起来像这样。

//define callback function to be used to check if value is empty
function empty_filter($var) {
    if(!empty($var)) return $var;
}

$filter_definitions = array(
    'thread' => array('filter'    => FILTER_CALLBACK,
                      'options'   => 'empty_filter',
                      'error_message'    => 'This field cannot be empty.'), // filter_var_array will ignore this, but you can specify messaging here.
    'winkey' => array('filter'    => FILTER_CALLBACK,
                      'options'   => 'empty_filter',
                      'error_message'    => 'This field cannot be empty.'),
    'dkey' => array('filter'    => FILTER_CALLBACK,
                      'options'   => 'empty_filter',
                      'error_message'    => 'This field cannot be empty.'),
    'winrew' => array('filter'    => FILTER_CALLBACK,
                      'options'   => 'empty_filter',
                      'error_message'    => 'This field cannot be empty.'),
    'drew' => array('filter'    => FILTER_CALLBACK,
                      'options'   => 'empty_filter',
                      'error_message'    => 'This field cannot be empty.')
);

$filtered_post = filter_input_array(INPUT_POST, $filter_definitions, true);

// items not passing filter will show as NULL, so we check for NULL
if(in_array(NULL, $filtered_post)) {
    // you can walk through the filtered array an echo out error messages
    foreach($filtered_post as $key => $value) {
        if(is_null($value)) {
            echo $key . ': ' . $filter_definitions[$key]['error_message'] . '<br>';
        }
    }
} else {
   // validation passed
   // do whatever comes next
}

这里注意,你可以为输入数组中的每个键定义不同的过滤条件,比如你想验证一个字段是电子邮件地址,一个是IP地址,一个是正则表达式,甚至您自己的自定义验证,您可以指定验证标准并一次性完成。

一旦您提交表单,您提到的 $_POST 中的所有变量都会被设置。如果未发出 POST 个请求,将设置其中的 none 个。

我认为您正在寻找 empty() 而不是测试是否有任何变量为空。

当您提交表单时,操作页面上的值消失了。 这意味着某些东西来自输入,它可能是空字符串,但您可以将此空字符串视为数字中的零,它没有值但有数字。与您的情况相同,值会出现在操作页面上,这就是每次打印值的原因。

当表单未提交时,您现在可以通过在新选项卡中打开操作页面进行检查。不会打印平均值不会出现在操作页面上。您的代码应如下所示。

if ( isset( $_POST['thread'],
    $_POST['winkey'],
    $_POST['dkey'],
    $_POST['winrew'],
    $_POST['drew'] )===true ) {
    if ($_POST['thread'] == "" and $_POST['winkey'] == "" and $_POST['dkey'] == "" and $_POST['winrew'] == "" and $_POST['drew'] == ""){
        echo "no";
    } 
    else{
            echo $_POST['thread'];
            echo $_POST['winkey'];
            echo $_POST['dkey'];
            echo $_POST['winrew'];
            echo $_POST['drew'];
    }

}
else echo 'no';