Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml\Math;
   6  
   7  use Phpml\Exception\InvalidArgumentException;
   8  
   9  class Comparison
  10  {
  11      /**
  12       * @param mixed $a
  13       * @param mixed $b
  14       *
  15       * @throws InvalidArgumentException
  16       */
  17      public static function compare($a, $b, string $operator): bool
  18      {
  19          switch ($operator) {
  20              case '>':
  21                  return $a > $b;
  22              case '>=':
  23                  return $a >= $b;
  24              case '=':
  25              case '==':
  26                  return $a == $b;
  27              case '===':
  28                  return $a === $b;
  29              case '<=':
  30                  return $a <= $b;
  31              case '<':
  32                  return $a < $b;
  33              case '!=':
  34              case '<>':
  35                  return $a != $b;
  36              case '!==':
  37                  return $a !== $b;
  38              default:
  39                  throw new InvalidArgumentException(sprintf('Invalid operator "%s" provided', $operator));
  40          }
  41      }
  42  }