Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.