2

I'm working on a PHP script that reads information from a JSON API, the purpose is to collect the information from the API and put it into a CSV file. I want to exclude certain pieces of information, so far I've only had one exclusion and it worked great but now I'm trying to add a second and can't seem to get it to work correctly. Is this the correct way to join the two conditions together?

$structure_type = $data['sam_data']['registration']['corporateStructureName'];
if($structure_type != "U.S. Government Entity" OR "Non-Profit Organization"){
}

I'm trying to make this so if either one of those phrases comes up under corporateStructureName then it is excluded. I thought using OR was correct but it doesn't seem to function.

3
  • in_array() or 2 conditions if($st != 'something' && $st != 'something else') Commented Oct 23, 2016 at 22:19
  • 2
    Are you really sure that you cannot find the answer to this yourself given the fine documentation and endless examples on the internet? Commented Oct 23, 2016 at 22:20
  • php.net/manual/en/language.operators.precedence.php Commented Oct 23, 2016 at 22:24

2 Answers 2

2

You need to have complete expressions either side of the OR.

if (!($structure_type == "U.S. Government Entity" OR
      $structure_type == "Non-Profit Organization")){
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh this worked, I didn't realize it had to be complete on both sides. Thank you for taking the time to answer this question, I learned something new because of this. I appreciate your time sir.
0

Another option, which is cleaner and more readable, is in_array($needle, $haystack):

<?php
$structureTypes = ['U.S. Government Entity', 'Non-Profit Organization'];
if (!in_array($structureType, $structureTypes)) {
    // ...
}

Compared to:

<?php
if (!($structureType == 'U.S. Government Entity' || $structureType == 'Non-Profit Organization')) {
    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.