0

My goal in the below code is to cycle through the IF statement and have the function return a True value if the IF statement holds true throughout all the iterations? If during any iteration the evaluation is false I want to immediately return False. Am I writing this correctly? I get confused with the order of operations within the IF statement. Is there a simpler more elegant way to write this?

function checkGroup($groupNum, $names, $namesNow, $$group, $groupSize, $i){
  for ($m=0; $m < count($$group); ++$m){
    //checks to make sure current person doesn't conflict with anyone else in current group
    if ($namesNow[$names[$i]][$$group[$m]] < $max ){
      continue;
    }
    else {
      return false;
      break;
    }
  }
  return true;
}
3
  • Why would you need variable variable? I mean $$group. Commented Jan 1, 2021 at 6:53
  • Double $ must be a typo when copying code to SO, as it's invalid in this context. Commented Jan 1, 2021 at 8:35
  • $$group is a variable variable. php.net/manual/en/language.variables.variable.php the code for that is below. it helps me create arrays named group1, group2, etc. $groupNum = 1; $group = "group".$groupNum; $$group = array(); Commented Jan 1, 2021 at 15:21

2 Answers 2

1

why not you try like this, revert the if condition

function checkGroup($groupNum, $names, $namesNow, $$group, $groupSize, $i){
   for ($m=0; $m < count($$group); ++$m){
   //checks to make sure current person doesn't conflict with anyone else in current group
      if ($namesNow[$names[$i]][$$group[$m]] >= $max ){
         return false;
      }
    }
  }
  return true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

No need for break after returning, though.
Thanks - i like that . i think you're right in that it is a little more strightforward
0

You can actually do something like this

function checkGroup($groupNum, $names, $namesNow, $group, $groupSize, $i){
    $ret = true;
    for ($m=0; $m < count($group); ++$m /* or $m++? */){
        //checks to make sure current person doesn't conflict with anyone else in current group
        if (!($namesNow[$names[$i]][$group[$m]] < $max)){
            $ret = false;
            break;
        }
    }
    return $ret;
}

1 Comment

thanks, I was thinking about something like that. i like it

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.