0

The following string (which comes from database) is stored as a variable,

str = "if($me == 1) 
{
 return array(9=>true,10=>true,11=>true)
} 
else 
 {
  return  array(9=>false,10=>false,11=>false)
 }"

Since the script is dynamic... so need to find output from this by passing a variable. can anyone help me how to do this.

2
  • 3
    I have to ask. Why are you storing PHP in the database? Commented Aug 1, 2013 at 10:04
  • I don't understand why it would need to come from a database? The script being dynamic won't depend on wherever it's from. Commented Aug 1, 2013 at 10:07

2 Answers 2

5

eval() will do what you want.

eval($string);

I really wouldn't suggest storing php in your database though as there are normally much more predictable, readable, understandable and maintainable ways of handling issues where you dynamically run different code based on results.

Sign up to request clarification or add additional context in comments.

Comments

0

You could do something like this:

<?php eval('?>' . $str . '<?php'); ?>

However, eval is not a great way of executing code. I wouldn't suggest storing php code in a database anyways as it makes it very difficult to debug, and it can be insecure, as it could contain malicious code.

Also checkout why lots of people say that 'Eval is Evil':

https://blogs.msdn.com/b/ericlippert/archive/2003/11/01/53329.aspx?Redirected=true https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/

1 Comment

eval() deoesnt work... if condition is stored in that variable

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.