jackerybakery Posted August 26, 2010 Share Posted August 26, 2010 Hello,I would like to create a function where I can pass an inequality [e.g.(<,>,<=,>=)] as an argument. Is this possible at all, and if so how? Tried a few ways but didn't manage to get it working.Thanks,Jack Link to comment Share on other sites More sharing options...
justsomeguy Posted August 26, 2010 Share Posted August 26, 2010 You'll need to pass it as a string, and then you can use eval to execute the comparison. Link to comment Share on other sites More sharing options...
Ingolme Posted August 27, 2010 Share Posted August 27, 2010 I haven't tested it, but I think the expression should return a boolean value. You probably can pass it as a parameter by having it between parenthesis or assigning it to a variable: someFunction(($a < $b)); OR $n = ($a < $b);someFunction($n); Link to comment Share on other sites More sharing options...
justsomeguy Posted August 27, 2010 Share Posted August 27, 2010 I was thinking more along the lines of a generic function to do some sort of comparison, e.g.: function compare($operator, $operand1, $operand2){ eval('$result = ' . $operand1 . $operator . $operand2); return $result;}if (compare('>=', $var1, $var2)){ ...} I'm not sure of the utility of that, but I guess I've heard of stranger things. Link to comment Share on other sites More sharing options...
Synook Posted August 27, 2010 Share Posted August 27, 2010 You can also pass a lambda function (PHP 5.3): function compare($a, $b, $f) { return $f($a, $b);}compare(1, 2, function($a, $b) { return $a > $b; }); //false Link to comment Share on other sites More sharing options...
jackerybakery Posted August 28, 2010 Author Share Posted August 28, 2010 Thanks very much for the feedback, I'll have a look and get back to you with how it goes. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.