php if !isset 多个 OR 条件

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

if !isset multiple OR conditions

php

提问by Depetrify

I cannot get this to work for the life of me, it is PHP.

我不能让它为我的生活工作,它是 PHP。

<?php
 if (!isset($_POST['ign']) || ($_POST['email'])) {

  echo "Please enter all of the values!";
    }

 else {

   echo "Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is         complete!";

    }
    ?>

I've also tried using !isset twice.

我也试过使用 !isset 两次。

回答by Diego Agulló

isset()accepts more than just oneparameter, so just pass as many variables as you need to check:

isset()接受的不仅仅是一个参数,因此只需传递您需要检查的尽可能多的变量:

<?php
    if (!isset($_POST['ign'], $_POST['email'])) {
        echo "Please enter all of the values!";
    }else{    
        echo "Thanks,". $_POST['ign'].", you will receive an email when the site is complete!";   
    }
?>

You could use empty()as well, but it doesn't accept more than a variable at a time.

您也可以使用empty(),但它一次只接受一个变量。

回答by sah

This is how I solved this issue:

我是这样解决这个问题的:

$expression = $_POST['ign'] || $_POST['email'] ;
if (!isset($expression) {
    echo "Please enter all of the values!";
}
else {
     echo "Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is                              
              complete!";
}

回答by vdbuilder

Simplest way I know of:

我所知道的最简单的方法:

<?php
if (isset($_POST['ign'], $_POST['email'])) {//do the fields exist
    if($_POST['ign'] && $_POST['email']){ //do the fields contain data
        echo ("Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is complete!");
    }
    else {
        echo ("Please enter all of the values!");
    }
}
else {
    echo ("Error in form data!");
}
?>

Edit: Corrected the code to show the form data and empty values errors seperatly.

编辑:更正了代码以分别显示表单数据和空值错误。

Explanation: The first if statement checks that the submitted form contained two fields, ign and email. This is done to stop the second if statement , in the case that ign or email weren't passed in at all, from throwing an error(message would be printed to server logs). The second if statement checks the values of ign and email to see if they contain data.

说明:第一个 if 语句检查提交的表单是否包含两个字段,ign 和 email。这样做是为了阻止第二个 if 语句,在根本没有传入 ign 或 email 的情况下,抛出错误(消息将打印到服务器日志)。第二个 if 语句检查 ign 和 email 的值,看它们是否包含数据。

回答by SAJINA C

Try this:

尝试这个:

<?php
 if (!isset($_POST['ign']) || isset($_POST['email'])) {
  echo "Please enter all of the values!";
    }
 else {
   echo "Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is         complete!";
    }
    ?>

回答by Manigandan Arjunan

 isset($_POST['ign'],$_POST['email']));

and then check for the empty values.

然后检查空值。

回答by Rajib.Hassan

You can try this code:

你可以试试这个代码:

<?php
    if(!isset($_POST['ign'], $_POST['email'])) {
        echo "Please enter all of the values!";
    } else {
        echo "Thanks, " . $_POST['ign'] . ", you will receive an email when the site is complete!";
    }
?>