Execute plain text as PHP code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Magma828
Forum Newbie
Posts: 2
Joined: Mon Jan 11, 2010 7:13 am

Execute plain text as PHP code

Post 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!!
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Execute plain text as PHP code

Post by Grizzzzzzzzzz »

why can't you use functions?
Magma828
Forum Newbie
Posts: 2
Joined: Mon Jan 11, 2010 7:13 am

Re: Execute plain text as PHP code

Post 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");
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Execute plain text as PHP code

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Execute plain text as PHP code

Post 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
}
 
Post Reply