Page 1 of 1
PHP Eval()
Posted: Mon Mar 29, 2010 3:53 pm
by mblack
Hi
I am trying to develop a good utility function and I'm not sure how to get it to work in php. I think I need to use the eval() function but I can't get it to work.
This is what I'm trying to accomplish:
Code: Select all
$value1 = 5;
$operator = '>=';
$value 2 = 3;
//if(5 >= 3)
if(eval($value1 .' '. $operator .' '. $value2))
{
//do something
}
That doesn't work so I tried hard coding my string
Code: Select all
eval('1 == 1');
Parse error: syntax error, unexpected $end in Utilities.php(124) : eval()'d code on line 1
Any help?
Re: PHP Eval()
Posted: Mon Mar 29, 2010 3:57 pm
by AbraCadaver
Same rules apply to evaled code. Try a semicolon:
So:
Code: Select all
$value1 = 5;
$operator = '>=';
$value2 = 3;
eval("if($value1 $operator $value2) { echo 'HI'; }");
Re: PHP Eval()
Posted: Mon Mar 29, 2010 4:16 pm
by mblack
Thanks for the quick response!
Is this the best way to go about doing things or is there a better way? What I'm trying to do is create a function that can be called to help with multiple comparisons. In the code examples below I want to be able to multiple comparisons where if any of the conditions are met then a text box would get disabled. Is there a better way to accomplish this or am I on the right track?
Sample code that would call the below function would be:
Code: Select all
<input type="text" Disable($value1, '>=' $value2, $value3, '<=' $value4, $value5, '==' $value6) />
Code: Select all
function Disable()
{
//The arguments should always be in sets of 3 in the format: value1, operator, value2
//The argument between the sets should either be: && (and), || (or)
$numargs = func_num_args();
$arg_list = func_get_args();
$flag = false;
//Make sure that the number of arguments are in sets of three
if($numargs AND 2)
{
for($i=0; $flag == false && $i<$numargs; ++$i)
{
//Check to make sure there is still three arguments to use in the comparison
if(isset($arg_list[$i + 2]))
{
$value1 = $arg_list[$i];
$operator = $arg_list[++$i];
$value2 = $arg_list[++$i];
eval("if($value1 $operator $value2) { \" $flag=true \"; }");
}
}
if($flag)
{
return 'style="background-color:#cfcfcf;" disabled="disabled" ';
}
}
}
Re: PHP Eval()
Posted: Tue Mar 30, 2010 9:34 am
by AbraCadaver
I'm not sure about best or better, but here is almost the same look and feel with a lot less code:
Code: Select all
echo '<input type="text"' . Disable($value1 >= $value2 || $value3 <= $value4 || $value5 == $value6) . ' />';
function Disable($disable)
{
if($disable) {
return ' style="background-color:#cfcfcf;" disabled="disabled"';
}
}
Or in HTML:
Code: Select all
<input type="text"<?php echo Disable($value1 >= $value2 || $value3 <= $value4 || $value5 == $value6); ?> />
Re: PHP Eval()
Posted: Tue Mar 30, 2010 1:06 pm
by josh
mblack wrote:
This is what I'm trying to accomplish:
Code: Select all
$value1 = 5;
$operator = '>=';
$value 2 = 3;
if(eval($value1 .' '. $operator .' '. $value2))
{
//do something
}
Code: Select all
class Compare
{
const LESS = 'less';
const EQUAL = 'equal';
const GREAT = 'great';
static function compare( $left, $right, $type = 'equal' )
{
switch( $type )
{
case 'less':
return $left < $right;
case 'equal';
return $left == $right;
case 'great':
return $left > $right;
}
throw new Exception('invalid operator');
}
}
Use like this..
Code: Select all
Compare::compare(1,2,Compare::LESS)
Re: PHP Eval()
Posted: Tue Mar 30, 2010 1:07 pm
by Benjamin

Moved to PHP - Code