检查多个条件是空的PHP

I want to check the user has selected any one of the input field, I 've tried like ,

 if(empty($_POST["month"]) and  ($_POST["eid"]))
 {
 ...
 }

But the condition is true when I'm using || opertator and the condition is false when I'm using &&.Why it is not working for && operator.How can I solve this?

I want to check the user has selected any one of the input field

Based on above statement, you can simply check for using isset():

if(isset($_POST["month"]) || isset($_POST["eid"]))

Another way of checking:

Has the user filled at least one of the fields is basically asking Are all the fields empty? If not - the user has checked at least one field, otherwise - none.

So simply use the && operator and add ! to the whole condition.

if(! ( empty($_POST['field1']) && empty($_POST['field2']) ... ) ) {
}

Edit: OP's condition:

I've three set of filelds like date1 and date2 ,id and date, month If user selects nothing from these three sets I want to display a message if the user select any one of the sets they can go in.

I hope I have understood you right. If not, please share an example.

  • set 1 - date1 and date2
  • set 2 - id and date
  • set 3 - month

Code:

if( (empty($_POST['date1']) and empty($_POST['date2'])) or
    (empty($_POST['id']) and empty($_POST['date'])) or 
    (empty($_POST['month'])) ){
  echo 'your message here.';
} else {
  //the user can go in.
}

Basically, a statement like:

if ($condition1 || $condition2) {}

returns TRUE when $condition1 OR $condition2 is true.

And, a statement like:

if ($condition1 && $condition2) {}

returns TRUE when both $condition1 AND $condition2 are true.

In your case, you need to use:

if(! empty($_POST["month"]) || ! empty($_POST["eid"])) {
    // do something
}

You can use this for checking empty field, while you use && the code is not working because ($_POST["eid"]) have no rules.

Example :

//You check the month are empty and the eid are ... ?
if(empty($_POST["month"]) && ($_POST["eid"]))

//Use this if you want to check the "month" and "eid" are Empty
if(empty($_POST["month"]) && empty($_POST["eid"])) {
   ... your validation here ...
}

//You can you is_null too
if(is_null($_POST["month"]) && is_null($_POST["eid"])) {
   ... your validation here ...
}

If you want to validate only one between $_POST["month"] or $_POST["eid"] just change the && to ||.

A compact way to do it is to make an array of all the values then check if they are empty.

If(empty(array($_POST["month"], $_POST["eid"]))){
// They are both empty
}else{
// Not empty
}