Jump to content

[SOLVED] Help with passing php scripts in a variable and execute them


EchoFool

Recommended Posts

I have a code which alters quite frequently so i save the php code for the script in a database table, and assign it to a variable for the script to attempt to read it as normal scripting language without errors..

 

How ever i have a problem because this is pretty much what im trying here:

 

<?php
$function = 'If($me == 2){';

$function."echo 'fail';}Else{";
?>
<input type="checkbox" name="list[]" value="<?php echo $value;?>">
<?php 
}
?>

Now this obviously gives me an unexpected } error.. which is where im stuck.

 

The idea is the script would read it naturally as php like so based on what is in the $funcation variable:

 

<?php
If($me == 2){echo 'fail';}Else{
?>
<input type="checkbox" name="list[]" value="<?php echo $value;?>">
<?php 
}
?>

 

Any thoughts on how i can go about this ? If at all possible in php?

 

Hope you can help! :)

how do you mean it alters is it at all possible for say one or two vars to alter but the core code remain the same the you could store just those 2 in the database?

but if you want to do code inside a var checkout eval

you might be able to go say:

<?php
$function = "If($me == 2){

echo 'fail';}Else{
echo '<input type=\"checkbox\" name=\"list[]\" value=\"$value\">';
}";
eval($function);
?>

i think

 

Scott.

how do you mean it alters is it at all possible for say one or two vars to alter but the core code remain the same the you could store just those 2 in the database?

but if you want to do code inside a var checkout eval

you might be able to go say:

<?php
$function = "If($me == 2){

echo 'fail';}Else{
echo '<input type=\"checkbox\" name=\"list[]\" value=\"$value\">';
}";
eval($function);
?>

i think

 

Scott.

 

It alters from IF statements to while loop or neither depending on user's choice on what they need to view.

 

I tried eval and it returned:

 

Parse error: parse error : eval()'d code on line 1

 

Is there a way to echo the Eval() text inside it to see what is causing the error ?

looking at that i just was thinking you need to escape $'s like this

$function = "if(\$me == 2){

echo 'fail';}else{
echo '<input type=\"checkbox\" name=\"list[]\" value=\"\$value\">';
}";

also i think the if and else should be lower case but i don't think it matters

 

Scott.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.