BenGoldberg Posted May 25, 2011 Share Posted May 25, 2011 One more question. So I have a function with two parameters, $x and $y. Here it is. function dydx($x,$y){ $equation = 2 * $x; return $equation; } Now here's the problem. I want $equation to be user defined. Easy enough, I use a post command and I get $equation to equal whatever the user inputs. The problem is that if I get input from the user, I'm not sure how to take that input and then have the function parameters work on it. Like if the user inputs "3*$x + 2*$y", i want to be able to let the parameters for the function dydx act on it. How could I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/237400-letting-user-input-php-code-and-having-a-function-parameter-act-on-it/ Share on other sites More sharing options...
anupamsaha Posted May 25, 2011 Share Posted May 25, 2011 Read the eval() in PHP http://php.net/manual/en/function.eval.php. But, be very careful because anybody can input any PHP command to blow your server. Sanitize users' input before you use them. Quote Link to comment https://forums.phpfreaks.com/topic/237400-letting-user-input-php-code-and-having-a-function-parameter-act-on-it/#findComment-1219859 Share on other sites More sharing options...
BenGoldberg Posted May 26, 2011 Author Share Posted May 26, 2011 Eval seems to be the right function for my application, but for the life of me I can not get it to work. Here's the basic code I'm working with. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table> <tr> <td>Enter function y prime:</td> <td><input type="text" size="50" maxlength="" name="yprime" value="<?php echo (isset($_POST['yprime']) ? $_POST['yprime'] : '')?>" /></td> </tr> </table> </br> <input type="submit" name="rk" value="Submit" /> </form> <?php $yprime = strval( $_POST['yprime'] ); //saving the function as a string function dydx($x,$y){ eval("\$equation = \"$yprime\";"); return $equation; } echo dydx(3,2); //equals 0 when it should equal 9 (yprime being 3*$x) ?> (The example input i use for yprime is 3*$x) I've done hours of googling and researching how eval works, but no matter what I try, the function dydx() always returns 0. The code above is what I believe to be my best attempt at getting it to evaluate $yprime properly, but it doesn't work. Argh! Quote Link to comment https://forums.phpfreaks.com/topic/237400-letting-user-input-php-code-and-having-a-function-parameter-act-on-it/#findComment-1220362 Share on other sites More sharing options...
xyph Posted May 26, 2011 Share Posted May 26, 2011 It's dangerous, but <?php $eq = '3*$x + 2*$y'; $y = 3; $x = 2; echo eval( "return $eq;" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237400-letting-user-input-php-code-and-having-a-function-parameter-act-on-it/#findComment-1220455 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.