Dynamic Comparison Operators in PHP
How about a small class:
class compare
{
function is($op1,$op2,$c)
{
$meth = array('===' => 'type_equal', '<' => 'less_than');
if($method = $meth[$c]) {
return $this->$method($op1,$op2);
}
return null; // or throw excp.
}
function type_equal($op1,$op2)
{
return $op1 === $op2;
}
function less_than($op1,$op2)
{
return $op1 < $op2;
}
}
Dynamically select a comparison operator (=, =, etc) in a conditional?
function lt($a, $b)
{
return $a < $b;
}
...
$relops = Array(
'<' => 'lt',
...
);
echo $relops['<'](2, 3);
PHP Dynamic operator usage
You can write a function with a switch case inside. like that:
$operator = '<='; // this can be '<=' or '<' or '>=' etc.
// I need like this something
$value1 = 2;
$value2 = 3;
function dyn_compare ($var1, $operator, $var2) {
switch ($operator) {
case "=": return $var1 == $var2;
case "!=": return $var1 != $var2;
case ">=": return $var1 >= $var2;
case "<=": return $var1 <= $var2;
case ">": return $var1 > $var2;
case "<": return $var1 < $var2;
default: return true;
}
}
//Then you can use
if (dyn_compare($value1, $operator, $value2)) {
echo 'yes';
}
Alternatively you can work with match like @Justinas suggested. But this only works with php 8 and upwards
How to use condition operator pulled from database and added to script with PHP
The only way you can do what you show is with eval(). But it's generally frowned upon to use eval()
because it introduces lots of potential security vulnerabilities.
I would do this hard-coded, to avoid eval()
and make sure I can control the specific operators I want to support:
$cond_operator = "<"; // (but this operator actually pulled from database)
switch ($cond_operator) {
case "<":
if ( $actual_value < $score_value ) {
return $result;
}
break;
case ">":
if ( $actual_value > $score_value ) {
return $result;
}
break;
case "==":
if ( $actual_value == $score_value ) {
return $result;
}
break;
default:
trigger_error("Unsupported condition operator: '$cond_operator'");
}
Using a variable as an operator
No, that syntax isn't available. The best you could do would be an eval(), which would not be recommended, especially if the $e came from user input (ie, a form), or a switch statement with each operator as a case
switch($e)
{
case "||":
if($a>$b || $c>$d)
echo 'yes';
break;
}
Dynamic logical expression parsing/evaluation in PHP?
Check create_function, it creates an anonymous function from the string parameters passed, I'm not sure about its performance, but it's very flexible...
Can I use operator in variable
Look into the eval() function. Though, I would say you should look into closures and even arrow functions.
$operator = fn($x) => $x >= 90;
$val = 100;
if($operator($val)){
echo "Correct!";
}
Related Topics
How to Take a Photo and Send to Http Post Request with Android
How to Send Multiple Attachment in Single Mail in PHP
Convert Array of Paths into Ul List
Getting the Full Url Including the Query String After Hash
How to Round to Nearest Thousand
Composer Running Out of Memory on Every Project, MAC Os X
Secure and Flexible Cross-Domain Sessions
How to? Form Post to Multiple Locations
Eloquent Many-To-Many-To-Many - How to Load Distant Relation Easily
How to Get MySQL 8 to Run with Laravel
PHP Authentication with Multiple Domains and Subdomains
Error 405 (Method Not Allowed) Laravel 5
Uncompress a Gzip File from Curl, on PHP