Created
July 20, 2017 18:49
-
-
Save facelordgists/de0ff3da68a8d46429c54e2d79b8cfa4 to your computer and use it in GitHub Desktop.
PHP: Conditional compare function. Compare two values using conditional as an argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: | |
// conditional_compare("bar", "=" "bar"); //returns true | |
// conditional_compare(123, "=" 123); //returns true | |
// conditional_compare(200, ">" 100); //returns true | |
// conditional_compare(100, ">" 200); //returns false | |
function conditional_compare($var1, $op, $var2) { | |
switch ($op) { | |
case "=": return $var1 == $var2; | |
case "!=": return $var1 != $var2; | |
case ">=": return $var1 >= $var2; | |
case "<=": return $var1 <= $var2; | |
case ">": return $var1 > $var2; | |
case "<": return $var1 < $var2; | |
default: return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment