Jump to content

Arguments


jackerybakery

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...