Kvvaradha
Kvvaradha

Reputation: 852

The Below IF condition is not working properly

I have an if statement which is the below one. But its not working i dont know the reason.

 if ((($posted['pref_sal_brn'] != null || $posted['pref_sal_end'] != null)  &&  $posted['pref_sal_prd'] != -1) || $posted['pref_sal_optn'] != -1){
 // Success code 
 } else { 
  // Else Part 
 }

What's wrong in my code? Here I need either one in first two, and if either one is not empty than the third one should be compulsory. if these three should be true or the fourth one is true like this.

EDIT :

array(4) { ["pref_sal_brn"]=> string(3) "234" ["pref_sal_end"]=> string(3) "500" ["pref_sal_prd"]=> string(2) "47" ["pref_sal_optn"]=> string(2) "51"  }

this is the output, first two parameters are salary begin and end, thrid one is period option like day, month, weekly,

and the fourth one is some options like , negotiable, minimum wage, like that. so first two are integers, third fourth are drop down lists for the items.

Upvotes: 0

Views: 52

Answers (2)

Razen Shrestha
Razen Shrestha

Reputation: 29

Just tested your code in phpfiddle.org and it is working properly. it will print success as it is supposed to `

$posted['pref_sal_brn'] = "234";
$posted['pref_sal_prd'] = "47";
$posted['pref_sal_end'] = "500";
$posted['pref_sal_optn'] = "51";
if (( ($posted['pref_sal_brn'] != null || $posted['pref_sal_end'] != null) && $posted['pref_sal_prd'] != -1) || $posted['pref_sal_optn'] != -1) {
    echo 'success';
} else {
    echo 'failure';
}

?>

Upvotes: 1

Ceeee
Ceeee

Reputation: 1442

This answer is based on what i understand while reading and analyzing your question, Your question is kinda hard to understand but this is what i grasp so far. Correct me if im wrong

Based on my understanding you cannot have it with one liner condition. you might want to break them into separate ifs

You said:

  1. I need either one in first two(pref_sal_brn and pref_sal_end), and if either one is not empty
  2. then the third one should be compulsory. (do this if above is true)
  3. if these three should be true (I understand this that 1 and 2 above should be true)
  4. or the fourth one is true like this. (or if the fourth one is true then whole process is true)

Breaking them and turning them into code:

// CHECK FIRST IF THE pref_sal_optn is TRUE, IF IT IS TRUE THEN DISREGARD OTHER CONDITION
if ($posted['pref_sal_optn'] != -1) {
    // success
} 
// NOW CHECK IF pref_sal_brn OR pref_sal_end IS NOT NULL
elseif ($posted['pref_sal_brn'] != null || $posted['pref_sal_end'] != null) {
    // YOU DISCOVERED THAT EITHER ONE OF pref_sal_brn OR pref_sal_end IS NOT NULL THEN CHECK IF pref_sal_prd IS != -1 IF TRUE THEN DO SUCCESS AGAIN
    if ($posted['pref_sal_prd'] != -1) {
        // success
    } else {
        // error
    }
} else {
    // error
}

Upvotes: 0

Related Questions