shamuraq Posted March 24, 2009 Share Posted March 24, 2009 Hi all, I'm trying to programme a PHP script (i'm a newbie in need of a guidance) to generate random algebraic expression including the addition and subtraction symbol. After which the reflected randomized will also solve the problem. Randomising part: <tr> <td> <? $x = rand(0,20); $y = rand(0,20); $z = rand(0,20); $quote[] = "-"; $quote[] = "+"; $quotes[] = "-"; $quotes[] = "+"; $randomquote = rand(0, count($quote)-1); $randomquotes = rand(0,count($quotes)-1); //eg1 //2y-x+3=0 printf("$y y $quotes[$randomquotes] $x x $quote[$randomquote] $z = 0"); ?> </td> </tr> Solving the randomised equation (this is the problem): <tr> <td> <? $a= $y * ($quotes[$randomquotes]$x); print("$a"); ?></td> </tr> I am successful with all the randomized result but i cannot get PHP to recognize that the randomized operator (addition and subtraction) are actually real operators not just some symbol. Attached is the script for your perusal. Thank you... Link to comment https://forums.phpfreaks.com/topic/150948-solving-equation-through-expression/ Share on other sites More sharing options...
genericnumber1 Posted March 25, 2009 Share Posted March 25, 2009 There are many ways of doing what you're explaining. The simplest is just to use eval, but I won't say whether or not using eval is a good thing, because there are strong feelings both for and against it. If you want some other methods, google "parsing a string as a mathematical equation" (or math parser or mathematic expression evaluator, etc), it should give you a good starting point (though not necessarily a php one) if you wish to learn about how to avoid using eval and to help yourself answer the question whether it's worth all of the trouble to avoid the function. Link to comment https://forums.phpfreaks.com/topic/150948-solving-equation-through-expression/#findComment-793323 Share on other sites More sharing options...
Mark Baker Posted March 25, 2009 Share Posted March 25, 2009 If the equation you're generating is of the form 2y-x+3=0, and you want to calculate the values of x and y, then you'll need a little more than eval to do this.... you'll need to do a bit of mathematics. Start by figuring out how to do it on paper, then (once you know the method) you can try implementing it in PHP. Link to comment https://forums.phpfreaks.com/topic/150948-solving-equation-through-expression/#findComment-793421 Share on other sites More sharing options...
genericnumber1 Posted March 25, 2009 Share Posted March 25, 2009 If you look at his code, it doesn't seem to be in an algebraic form. He appears to have the values of x and y and wishes to just plug them into the formula and evaluate it - all completely doable with eval (again, if you want to use eval). Link to comment https://forums.phpfreaks.com/topic/150948-solving-equation-through-expression/#findComment-793872 Share on other sites More sharing options...
shamuraq Posted April 14, 2009 Author Share Posted April 14, 2009 Yes genericnumber1, I already have the value of x and y (i already have the working solution base on this particular template). However being a newbie and a dumbwit i must admit, what is eval()? seems like it has its downside. I apologise for the late respond (internet intermittent problem at my location). Link to comment https://forums.phpfreaks.com/topic/150948-solving-equation-through-expression/#findComment-810091 Share on other sites More sharing options...
Rithiur Posted April 19, 2009 Share Posted April 19, 2009 You could be using eval to evaluate the expression, using something like $a= eval("return $y * {$quotes[$randomquotes]}$x;"); Personally I feel that is not very good practice, since using eval tends to lead into bad programming practices and often even into security holes in PHP code (when, for example, evaluating input that comes from the user). A better way, in my opinion, to solve your problem would be to use something like a simple if clause, like if ($quotes[$randomquotes] == '-') { $a = $y * -$x; } else { $a = $y * $x; } Much easier to understand in code and doesn't make the simple issue overly complicated. If you want to write it in a bit shorter form, you can use the ternary operator like: $a = $y * ($quotes[$randomquotes] == '-' ? -$x : $x); (it checks whether the expression left of '?' is true, and if so, it evaluates and returns the value before the ':' character, and otherwise it evaluates and returns the value right from the ':' character) Link to comment https://forums.phpfreaks.com/topic/150948-solving-equation-through-expression/#findComment-813636 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.