Skip to main content
edited tags; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

concise alternative PHP function to switch control structureevaluate comparisons between two values, with debug logging

Post Migrated Here from stackoverflow.com (revisions)
Source Link
knot22
  • 469
  • 4
  • 12

concise alternative to switch control structure

The following method works fine but it is rather verbose:

private function performComparison($element, $operator, $value_allowed, $value_entered)
{
    switch($operator):
        case '>':
            $result = ($value_entered >  $value_allowed) ? true : false;
            break;
        case '>=':
            $result = ($value_entered >= $value_allowed) ? true : false;
            break;
        case '<':
            $result = ($value_entered <  $value_allowed) ? true : false;
            break;
        case '<=':
            $result = ($value_entered <= $value_allowed) ? true : false;
            break;
        case '=':
            $result = ($value_entered == $value_allowed) ? true : false;
            break;
    endswitch;
        $dateformat = 'n/j/Y';
        $debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
        echo '<pre>'.print_r($debug,1).'</pre>';
    return $result;
}

Note that the 3 lines of code just before the return $result; line are very helpful for debugging in the Development phase so I'd like to keep those.

In an attempt to make a more concise version of this method I came up with the following:

private function performComparison($element, $operator, $value_allowed, $value_entered)
{
    $comparisonFunctions = [
        '>' => function($value_allowed, $value_entered) { return ($value_entered >  $value_allowed) ? true : false; },
        '>='    => function($value_allowed, $value_entered) { return ($value_entered >= $value_allowed) ? true : false; },
        '<' => function($value_allowed, $value_entered) { return ($value_entered <  $value_allowed) ? true : false; },
        '<='    => function($value_allowed, $value_entered) { return ($value_entered <= $value_allowed) ? true : false; },
        '=' => function($value_allowed, $value_entered) { return ($value_entered == $value_allowed) ? true : false; }
    ];

    $result = array_key_exists($operator, $comparisonFunctions) ? $comparisonFunctions[$operator]($value_allowed, $value_entered) : null;
        $dateformat = 'n/j/Y';
        $debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
        echo '<pre>'.print_r($debug,1).'</pre>';
    return $result;
}

This version also works fine but I'm wondering if there are any ways it could be condensed further.