ostego
ostego

Reputation: 21

How to set a variable value based on conditions set by other variables

So I am new to PHP, and am currently just doing a little project as practice, I've managed to get down a few lines of code without falling over... but am a bit stuck here.

Essentially, what my script currently does is check three different variables that I have set (each a true/false option) and distinguishes if there is only one true option selected (out of the 3 options, only one can be true, the other 2 must be false). If only 1 value is set to true, the rest of the code runs; if multiple values are set to true, or no values are set to true, it shows an error prompt for the user.

Once this check is done, I wanted to then set the value of $name for example, based on records linked to the relevant variable that is true... This is what I have come up with, but it doesn't seem to work...

if ($value1 == "true") {$name = $result1;}

else if ($value2 == "true") {$name = $result2;}

else if ($value3 == "true") {$name = $result3;}

else exit (0)

So i essentially want to set the $name variable by identifying which of the 3 value variables is true, and then setting $name with the relevant variable retrieved in the $result

Any help would be appreciated. And before anyone goes off on one... I know I may sound a bit mad... but we all have to start somewhere!!

Thanks

Upvotes: 2

Views: 2037

Answers (2)

Dracony
Dracony

Reputation: 842

It would look much nicer with a switch:

switch(true){
   case $value1:
      $name = $result1;
      break;
   case $value2:
      $name = $result2;
      break;
   case $value3:
      $name = $result3;
      break;
   default:
      exit();
}

In case you need to make sure only one of the statements is true, validate that prior using this:

//In case you need to make there is only a single true statement
$found = false;
for($i=1; $i<4; $i++) {
   $value = "value".$i;
   if($$value) {
      if($found) {
          exit("More than one 'true' statement");
      }
      $found = true;
   }
}

Upvotes: 2

webketje
webketje

Reputation: 10986

Dracony's answer looks nice indeed, but will fail when multiple values are set to true. For more flexibility, you should consider mapping the values into arrays, and tracking the state (amount of values that are true) with a flag variable. Find a fully commented example that will satisfy all conditions below. Additionally, this code will work with arrays of any length (you can add conditions by simply putting more values in $values and $results).

// store the data in arrays for easier manipulation
$values = array($value1, $value2, $value3);
$results = array($result1, $result2, $result3);

// set a flag to check the state of the condition inside the loop
// it keeps track of the index for which the value is true
$flag = -1;
$name = NULL;

// use for and not foreach so we can easily track index
for ($i = 0; $i < count($values); $i++) {
  // if no index has been found 'true' yet, assign the current result.
  if ($values[$i] === true) {
    if ($flag === -1) {
      $flag = $i;
      $name = $results[$i];
    }
    // if a 'true' value has been found for another index
    // reset the name var & stop the loop
    if ($flag > -1 && $flag !== $i) {
      $name = NULL;
      break;
    }
  }   
}

if ($name) {
  // run code when only 1 value is true
} else {
  exit();
}

Upvotes: 0

Related Questions