title: Evaluate PHP logic stored in arrays
body: <h2>Objective</h2>
  
  <p>I'm trying to store complex comparisons as PHP arrays.  I then want to read the array to evaluate a group of variables and return whether the statement is true or false.</p>
  
  <h2>Question</h2>
  
  <p>How can I store complex comparisons so that they can later be read and evaluate the variables present at the time?  Is this the best way to accomplish this?  Your help is GREATLY appreciated.</p>
  
  <h2>Example, English</h2>
  
  <p>If($source = 200 OR $source = 300) AND ($age &lt; 10 OR $age > 60)  THAN return true
  Else return false</p>
  
  <p><strong>Notes:</strong>
  Variables can be any variable in the code, I used source and age as an example here. The nested arrays can be deeper or flatter than the example. </p>
  
  <h2>Example, Code</h2>
  
  <pre><code>$conditions = Array
  (
  [relation] =&gt; AND
  [0] =&gt; Array
      (
          [relation] =&gt; OR
          [0] =&gt; Array
              (
                  [var] =&gt; source
                  [compare] =&gt; =
                  [value] =&gt; 300
              )
  
          [1] =&gt; Array
              (
                  [var] =&gt; source
                  [compare] =&gt; =
                  [value] =&gt; 300
              )
  
      )
  
  [1] =&gt; Array
      (
          [relation] =&gt; OR
          [0] =&gt; Array
              (
                  [var] =&gt; age
                  [compare] =&gt; &lt;
                  [value] =&gt; 10
              )
  
          [1] =&gt; Array
              (
                  [var] =&gt; age
                  [compare] =&gt; &gt;
                  [value] =&gt; 60
              )
  
      )
  </code></pre>
  
  <p>)</p>
  
  <h1>SOLUTION</h1>
  
  <p>Based on the answer by @sam-grondahl
  Function can handle any nested logic you can throw at it.
  Accepts regex and numeric comparisons</p>
  
  <pre><code>$source = '300';
  $age = '60';
  
  $input_variables = array('source' =&gt; $source, 'age' =&gt; $age);
  
  $conditions = array(
                  'relation' =&gt; 'and',
                  array(
                          'relation' =&gt; 'and',
                          array(
                                'var' =&gt; 'source',
                                'compare' =&gt; 'regex',
                                'value' =&gt; '(200|300)+'
                                ),
                          array(
                                'relation' =&gt; 'OR',
                                  array(
                                        'var' =&gt; 'age',
                                        'compare' =&gt; '=',
                                        'value' =&gt; '59'
                                        ),
                                  array(
                                        'var' =&gt; 'age',
                                        'compare' =&gt; '=',
                                        'value' =&gt; '60'
                                        )
                                        )
                        ),
                  array(
                          'relation' =&gt; 'or',
                          array(
                                'var' =&gt; 'age',
                                'compare' =&gt; '&lt;',
                                'value' =&gt; '10'                                 
                                ),
                          array(
                                'var' =&gt; 'age',
                                'compare' =&gt; '&gt;=',
                                'value' =&gt; '60'
                                )
                        ),
                       array(
                                'var' =&gt; 'source',
                                'compare' =&gt; '=',
                                'value' =&gt; '300'
                                ),
  
                  );
  
  
  function evaluateConditions($condArray, $varablesArray){
  
  if(!$condArray || !is_array($condArray)){throw new Exception('Missing Conditions Input Array');}
  if(!$varablesArray || !is_array($varablesArray)){throw new Exception('Missing Variables Input Array');}
  
  //PULL VARIABLES FROM ARRAY
  foreach($varablesArray as $key_var =&gt; $value_var){
      ${$key_var} = $value_var;
  }
  
  
  ///PROCESS THE ARRAY THAT CONTAINS THE COMPARISON ARRAY
  if (!$condArray['relation']){
  
   //FORMAT COMPARE INPUT
   $condArray['compare'] = strtoupper(trim($condArray['compare']));
   $condArray['compare'] = preg_replace('/^=$/','==',$condArray['compare']);
  
   ///ACCEPTED COMPARE VALUES
   $accepted_compare_array = array('=','==','===','&lt;&gt;','!=','!==','&gt;','&gt;=','&lt;','&lt;=','REGEX');
  
   //VERIFY REQUIRED FIELDS ARE PRESENT
   if(!$condArray['var']){throw new Exception('Missing var');}
   if(!$condArray['compare']){throw new Exception('Missing compare');}
   if(!$condArray['value']){throw new Exception('Missing value');}
  
   //VERIFY ALL VALUES
   if($condArray['compare'] != 'REGEX' &amp;&amp; !is_numeric($condArray['value'])){throw new Exception('Value is Not Numeric');}
   if(!in_array($condArray['compare'], $accepted_compare_array)){throw new Exception('Invalid Compare Value');}
   if(!preg_match("/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/",$condArray['var'])){throw new Exception('Invalid Var');}
  
   if($condArray['compare'] == 'REGEX'){
      $condArray['compare'] = preg_replace('/\//','\/',$condArray['compare']);
      $eval_str = 'if(preg_match("/'.$condArray['value'].'/i", $'.$condArray['var'].')){return 1;} else{return 0;}'; 
   }
   else{   
      $eval_str = 'if($'.$condArray['var'].' '.$condArray['compare'].' '.$condArray['value'].'){return 1;} else{return 0;}';
   }
  
   return eval($eval_str);
  
   }
  else{
  
   $condArray['relation'] = strtoupper(trim($condArray['relation']));
  
   //VERIFY RELATION VALUE
   $accepted_relation_array = array('AND','OR');
   if(!in_array($condArray['relation'], $accepted_relation_array)){throw new Exception('Relation Invalid');}
  
   //VERIFY THAT AT LEAST TWO ARRAYS
   $array_count = 0;
   foreach($condArray as $condArray_var){
      if(is_array($condArray_var)){$array_count++;} 
   }
  
   if($array_count &lt; 2){throw new Exception('Missing Comparison Array');}
  
   }
  
  
  
  if (strtoupper($condArray['relation']) == 'OR') {
   unset($condArray['relation']);
  
   $eval_str = 'if(';
   for($i=0; $i &lt; count($condArray); $i++){
      $eval_str .= evaluateConditions($condArray[$i], $varablesArray);
      if($i != count($condArray)-1){$eval_str .= ' || ';}  
   }   
   $eval_str .= '){return 1;}else{return 0;}';
  
   return eval($eval_str);
  
   }
   if (strtoupper($condArray['relation']) == 'AND'){
   unset($condArray['relation']);
   $eval_str = 'if(';
   for($i=0; $i &lt; count($condArray); $i++){
      $eval_str .= evaluateConditions($condArray[$i], $varablesArray);
      if($i != count($condArray)-1){$eval_str .= ' &amp;&amp; ';}  
   }   
   $eval_str .= '){return 1;}else{return 0;}';
  
   return eval($eval_str);
  
   }
  throw new Exception('General Error Occurred');
  } //end of function
  
  /////
  
  try {
  echo evaluateConditions($conditions, $input_variables);
  } 
  catch (Exception $e) {
  echo 'Caught exception: ',  $e-&gt;getMessage(), "\n";
  }   
  </code></pre>