SHARE
TWEET

Untitled

a guest Jun 26th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isAnd($var, $value, $operator = '==')
  2. {
  3.     if(isset($var) && $var $operator $value)
  4.         return true;
  5. }
  6.  
  7. if(isAnd(1, 1, '===')) echo 'worked';
  8.      
  9. class compare
  10. {
  11.   function is($op1,$op2,$c)
  12.   {
  13.      $meth = array('===' => 'type_equal', '<' => 'less_than');
  14.      if($method = $meth[$c]) {
  15.         return $this->$method($op1,$op2);
  16.      }
  17.      return null; // or throw excp.
  18.   }
  19.   function type_equal($op1,$op2)
  20.   {
  21.       return $op1 === $op2;
  22.   }
  23.   function less_than($op1,$op2)
  24.   {
  25.       return $op1 < $op2;
  26.   }
  27. }
  28.      
  29. function num_cond ($var1, $op, $var2) {
  30.  
  31.     switch ($op) {
  32.         case "=":  return $var1 == $var2;
  33.         case "!=": return $var1 != $var2;
  34.         case ">=": return $var1 >= $var2;
  35.         case "<=": return $var1 <= $var2;
  36.         case ">":  return $var1 >  $var2;
  37.         case "<":  return $var1 <  $var2;
  38.     default:       return true;
  39.     }  
  40. }
  41.      
  42. $ops = array( "=", "!=", ">=", "<=", ">", "<" );
  43. $v1 = 1; $v2 = 5;
  44.  
  45. foreach ($ops as $op) {
  46.     if (num_cond($v1, $op, $v2)) echo "True  ($v1 $op $v2)n"; else echo "False ($v1 $op $v2)n";
  47. }
  48.      
  49. function isAnd($var, $value, $operator = '==') {
  50.     return isset($var) && $var $operator $value;
  51. }
  52.  
  53. isAnd($foo, 1, '===');
  54.      
  55. isset($foo) && isAnd($foo, 1, '===');
  56.      
  57. if(isset($var) && eval("return $var $operator $value"))
  58.     return true;
  59.      
  60. trait DynamicComparisons{
  61.  
  62. private $operatorToMethodTranslation = [
  63.     '=='  => 'equal',
  64.     '===' => 'totallyEqual',
  65.     '!='  => 'notEqual',
  66.     '>'   => 'greaterThan',
  67.     '<'   => 'lessThan',
  68. ];
  69.  
  70. protected function is($value_a, $operation, $value_b){
  71.  
  72.     if($method = $this->operatorToMethodTranslation[$operation]){
  73.         return $this->$method($value_a, $value_b);
  74.     }
  75.  
  76.     throw new Exception('Unknown Dynamic Operator.');
  77. }
  78.  
  79. private function equal($value_a, $value_b){
  80.     return $value_a == $value_b;
  81. }
  82.  
  83. private function totallyEqual($value_a, $value_b){
  84.     return $value_a === $value_b;
  85. }
  86.  
  87. private function notEqual($value_a, $value_b){
  88.     return $value_a != $value_b;
  89. }
  90.  
  91. private function greaterThan($value_a, $value_b){
  92.     return $value_a > $value_b;
  93. }
  94.  
  95. private function lessThan($value_a, $value_b){
  96.     return $value_a < $value_b;
  97. }
  98.  
  99. private function greaterThanOrEqual($value_a, $value_b){
  100.     return $value_a >= $value_b;
  101. }
  102.  
  103. private function lessThanOrEqual($value_a, $value_b){
  104.     return $value_a <= $value_b;
  105. }
  106.  
  107. }
  108.      
  109. function is_equal($a, $b) {
  110.   return $a==$b;
  111. }
  112. function is_same($a, $b) {
  113.   return $a===$b;
  114. }
  115. function is_greater_than($a, $b)
  116. ....
  117.  
  118. $compare='is_equal';
  119. if ($compare($a, $b)) {
  120.    ....
  121.      
  122. $b = 10;
  123. $c = '+';
  124. $p = $a . $c. $b; // Forming a String equation
  125. $p = eval('return '.$p.';'); // Evaluating the Equation
  126. echo $p;
  127.      
  128. 15
  129.      
  130. $b = 10;
  131. $c = '==';
  132. $p = $a . $c. $b;
  133. $p = eval('return '.$p.';');
  134. echo $p;
  135.      
  136. false
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top