Page 1 of 1

Execute plain text as PHP code

Posted: Mon Jan 11, 2010 7:18 am
by Magma828
Hi everyone!

What I want to do sounds quite simple, but I have no idea how to do it. I want to store PHP code inside a variable, then take the code out of the variable and execute it. For example:

Code: Select all

 $codeToExecute = "if($food === 'pizza') echo 'yum';";
would execute the code

Code: Select all

 if($food === 'pizza') echo 'yum';
Does PHP even have this functionality? I'd google it but I don't know what it's called..

Thank you!!

Re: Execute plain text as PHP code

Posted: Mon Jan 11, 2010 7:36 am
by Grizzzzzzzzzz
why can't you use functions?

Re: Execute plain text as PHP code

Posted: Mon Jan 11, 2010 8:00 am
by Magma828
Grizzzzzzzzzz wrote:why can't you use functions?
I don't understand why I would need functions for this. I don't want to put code in a variable to save code duplication or anything.

I didn't really want to blast you all with a wall of text about what this is for but I guess I'll have to..

I'm storing a set of validation rules for each field in a form, in a database. When a field needs to be validated, PHP will check the database for any validation rules (which will be in the form of PHP code) and execute them. I don't want to set a static list of possible validation rules because I'm making a highly configurable CMS.

The database would store something like this:

Code: Select all

[Field]  "ID"
[Rule]  "is_numeric({datum}) === false"
[Response]  "Animal ID must be a number"
 
[Field]  "Animal"
[Rule]  "{datum} === 'cat'"
[Response]  "A cat is not an animal"
 
[Field]  "Animal"
[Rule]  "{datum} === ''"
[Response]  "Please enter something"
Then PHP would replace {datum} with the variable in subject (eg, $_POST['animal']), execute the code, and if the operation returns true then send an error message.

So it would generate and execute this code:

Code: Select all

if((is_numeric($_POST['id']) === false) === true) {
  die("Animal ID must be a number");
}
if(($_POST['animal'] === 'cat') === true) {
  die("A cat is not an animal");
}
if(($_POST['animal'] === '') === true) {
  die("Please enter something");
}

Re: Execute plain text as PHP code

Posted: Mon Jan 11, 2010 10:07 am
by AbraCadaver
To answer your question:

Code: Select all

eval($codeToExecute);
But to Grizzzzzzzzzz's point, why not build a function to do this. If there are multiple rules for a field, then you can pass in an array of rules:

Code: Select all

function validator($field, $rule) {
    //use field and rule to validate
    //return true or false or a $response string
}
You may want to tweak your rules as they seem overly complex:

Code: Select all

[Field]  "ID"
[Rule]  "is_numeric"
[Response]  "Animal ID must be a number"
 
[Field]  "Animal"
[Rule]  "=cat"
[Response]  "A cat is not an animal"
 
[Field]  "Animal"
[Rule]  "not_empty"
[Response]  "Please enter something"
We could probably come up with a pretty cool function if you're flexible.

Re: Execute plain text as PHP code

Posted: Mon Jan 11, 2010 10:40 am
by Grizzzzzzzzzz
Magma828 wrote:
Grizzzzzzzzzz wrote:why can't you use functions?
I don't understand why I would need functions for this. I don't want to put code in a variable to save code duplication or anything.
AbraCadaver wrote: But to Grizzzzzzzzzz's point, why not build a function to do this. If there are multiple rules for a field, then you can pass in an array of rules:

Code: Select all

 
function validator($field, $rule) {
    //use field and rule to validate
    //return true or false or a $response string
}