1

I have the code below taken partially inside a function:

$dynamic_comparison = '';  
if(($from == '')&&($to == '')){
    $dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
    $dynamic_comparison = '($row >= $from) && ($row <= $to)';
}else if(($from != '')&&($to == '')){
    $dynamic_comparison = '($row >= $from)';
}else if(($from == '')&&($to != '')){
    $dynamic_comparison = '($row <= $to)';
}

$form, $to and $row are the parameters of the function.

I want to evaluate $dynamic_comparison into something like this:

if($dynamic_comparison){
    //A bunch of code here...
}

I tried:

if(eval($dynamic_comparison)){
    //A bunch of code here...
}

It throws an error. How to get this right?

0

2 Answers 2

1

eval() is bad idea imho. You can use anonymous function, like this

$dynamic_comparison = function($row, $to, $from) { return false };  
if(($from == '')&&($to == '')){
    $dynamic_comparison = function($row, $to, $from) { return true;};
}else if(($from != '')&&($to != '')){
    $dynamic_comparison = function($row, $to, $from) { return ($row >= $from) && ($row <= $to);};
}else if(($from != '')&&($to == '')){
    $dynamic_comparison = function($row, $to, $from) { return ($row >= $from);};
}else if(($from == '')&&($to != '')){
    $dynamic_comparison = function($row, $to, $from) { return ($row <= $to);};
}

And use it like

if($dynamic_comparison($row, $to, $from))...

If you want to use eval anyway:

$dynamic_comparison = 'return false;';  
if(($from == '')&&($to == '')){
    $dynamic_comparison = 'return true;';
}else if(($from != '')&&($to != '')){
    $dynamic_comparison = 'return ($row >= $from) && ($row <= $to);';
}else if(($from != '')&&($to == '')){
    $dynamic_comparison = 'return ($row >= $from);';
}else if(($from == '')&&($to != '')){
    $dynamic_comparison = 'return ($row <= $to);';
}

if(eval($dynamic_comparison)){//$to,$from,$row must be available in this scope
Sign up to request clarification or add additional context in comments.

4 Comments

I want to know, why is it a bad idea? :)
Because usage may have unexpected side effects
@Aliyah ok, if you want use eval, you must write string like 'return ($row >= $from) && ($row <= $to);' or 'return 1;' - this will work with eval
I still dont get it :(
0

i think you have to use double quotes insted of single quotes for your string like this :

<?php 
$dynamic_comparison = '';  
if(($from == '')&&($to == '')){
    $dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
    $dynamic_comparison = "($row >= $from) && ($row <= $to)";
}else if(($from != '')&&($to == '')){
    $dynamic_comparison = "($row >= $from)";
}else if(($from == '')&&($to != '')){
    $dynamic_comparison = "($row <= $to)";
}

if you don't have $row initialezed at this moment , escape $ character :

<?php 
    $dynamic_comparison = '';  
    if(($from == '')&&($to == '')){
        $dynamic_comparison = 1;
    }else if(($from != '')&&($to != '')){
        $dynamic_comparison = "(\$row >= $from) && (\$row <= $to)";
    }else if(($from != '')&&($to == '')){
        $dynamic_comparison = "(\$row >= $from)";
    }else if(($from == '')&&($to != '')){
        $dynamic_comparison = "(\$row <= $to)";
    }

see eval() documentation : http://php.net/manual/en/function.eval.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.