I am trying to modularise a lengthy if..else
function.
$condition = "$a < $b";
if($condition)
{
$c++;
}
Is there any way of translating the literal string into a logical expression?
I am trying to modularise a lengthy if..else function.
You don't need to put the condition in a string for that, just store the boolean true
or false
:
$condition = ($a < $b);
if($condition)
{
$c++;
}
the values of $a and $b may change between definition of $condition and its usage
One solution would be a Closure (assuming that definition and usage are happening in the same scope):
$condition = function() use (&$a, &$b) {
return $a < $b;
}
$a = 1;
$b = 2;
if ($condition()) {
echo 'a is less than b';
}
But I don't know if this makes sense for you without remotely knowing what you are trying to accomplish.