MasterACE14 Posted May 20, 2007 Share Posted May 20, 2007 I was wondering how(thats if it's possible) you can put a Mathematical Operator in a Variable. Let me try and show you what I am trying to explain. The Following Example is what I am trying to do, except instead of using a set Mathematical Operator(The Plus sign in this case) I would like to have the 4 Main Mathematical Operators each in their own variable and have 1 randomly chosen to put in between the x and y variable in the result variable. <?php $x = 1; $y = 1; $result = ($x + $y); echo("$result"); ?> Something along these lines, even though I know it doesn't work. <?php $plus = +; $minus = -; $divide = /; $multiply = *; rand(variable); $x = 1; $y = 1; $result = ($x rand() $y); echo("$result"); ?> Something Like that even though I know that, that is way off what it should be. please go easy on me, I'm just a noob Regards MasterACE14 Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/ Share on other sites More sharing options...
OOP Posted May 20, 2007 Share Posted May 20, 2007 Hi, You can do simple thing as below...see if it is OK with you <?php function performOperation($operand1,$operand2,$operation){ $result = 0; $output = "The result of $operand1 $operation $operand2 is:"; switch($operation){ case '+': $result = $operand1 + $operand2; break; case '-': $result = $operand1 - $operand2; break; case '*': $result = $operand1 * $operand2; break; case '/': if ( $operand2 != 0 ){ $result = $operand1 / $operand2; }else{ echo 'Division by zero error'; } break; } echo $output.$result; } $operations = array(1=>'+',2=>'-',3=>'*',4=>'/'); $index = rand(1,4); $current_operation = $operations[$index]; $x = 2; $y = 2; performOperation($x,$y,$current_operation); Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/#findComment-257400 Share on other sites More sharing options...
MasterACE14 Posted May 20, 2007 Author Share Posted May 20, 2007 Excellant! That works perfectly! Thanks Heap man, Much Appreciated Regards MasterACE14 Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/#findComment-257403 Share on other sites More sharing options...
neel_basu Posted May 20, 2007 Share Posted May 20, 2007 Ya its of course possible. <?php $op = array('+', '-', '*', '/'); eval("\$res = 15".$op[rand(0, 3)]."5;"); echo $res; ?> And it can be done very simply. Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/#findComment-257406 Share on other sites More sharing options...
OOP Posted May 20, 2007 Share Posted May 20, 2007 You are most welcome my friend Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/#findComment-257407 Share on other sites More sharing options...
MasterACE14 Posted May 20, 2007 Author Share Posted May 20, 2007 Thankyou Both of you. I've Tried your script neel_basu, and I get: Parse error: parse error, unexpected '=' in C:\WebServer\Abyss Web Server\htdocs\phpeval\index.php(52) : eval()'d code(2) : eval()'d code on line 1 "I run it through my PHP testing lab, makes life easier ^^ " Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/#findComment-257417 Share on other sites More sharing options...
neel_basu Posted May 20, 2007 Share Posted May 20, 2007 I donno whats the problem over there But that code works in my PC Ok try this code <?php $op = array('+', '-', '*', '/'); eval("echo 15".$op[rand(0, 3)]."5;"); //echo $res; ?> Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/#findComment-257424 Share on other sites More sharing options...
MasterACE14 Posted May 20, 2007 Author Share Posted May 20, 2007 yep, that works , thanks Link to comment https://forums.phpfreaks.com/topic/52190-solved-random-mathematical-operators/#findComment-257427 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.