chronister Posted January 25, 2009 Share Posted January 25, 2009 Hello, I am trying to "evaluate" code passed through an array. basically it looks like this... $data = array('dependsOn'=>'name == bob'); I want to be able to take the name == bob part of the array and actually use that part in an expression. I used explode to break it apart, but the only way I can figure out how to determine the operator is to use a switch statement to check it over. Is there a way to take it and use it inside the var?? e.g. <?php $parts = explode(' ', $v['dependsOn']); if($_POST[$parts[0]]. $parts[1]. $parts[2]){ echo 'true'; } ?> to turn it into <?php if($_POST['name'] == 'bob') { //do something here because it is true. } ?> eval does not seem to work here as the string passed to eval should have the entire if statement inside it, and it appears that a == inside a var will not be used as an operand. What would be the best way to do this kind of thing? Thanks in advance. nate Link to comment https://forums.phpfreaks.com/topic/142373-question-about-eval/ Share on other sites More sharing options...
wrs Posted January 25, 2009 Share Posted January 25, 2009 The problem here is that your eval() statement must be sane PHP. eval('if ($user == "something") return true; else return false;') Now your eval statement will return true or false, and can be placed within your if() block. $o = 'name == bob'; $parts = explode(' ', $o); $user = Array( 'name' => 'bob', ); $str = 'if ($user[\''.$parts[0].'\'] '.$parts[1].' \''.$parts[2].'\') return true; else return false;'; if (eval($str)) echo 'name matches'; else echo 'name does not match'; Link to comment https://forums.phpfreaks.com/topic/142373-question-about-eval/#findComment-745975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.