I have the below code snippet which will cast an input to a float and create a variable maths operator of either > or < based on whether the float is a positive or negative number.
// allow custom variance from user input
if (isset($request->custom_variance)) {
$variance_amount = floatval($request->custom_variance);
($variance_amount < 0) ? $operator = " < " : $operator = " > ";
} else {
// set default
$variance_amount = floatval(-50);
$operator = " < ";
}
I then use these variables in an if statement, I have dumped here to show the output:
var_dump((floatval($value_one) - floatval($value_two)).$operator.$variance_amount);
// returns "-51.35 < -50"
If I hard code the values I get this (It works as I want a Boolean)
var_dump((floatval($value_one) - floatval($value_two)) < -50);
// returns true
I have done some reading on here about this and it seems like it isn't working as my operator is being interpreted as a string but I would like to avoid using the eval() function if possible.
Any suggestions?