wolfcry Posted January 2, 2012 Share Posted January 2, 2012 Hey all, I'm building a pretty extensive calculator program for my users but I'm stuck on the best way to doe the math operation. I currently have all data stored in a database (such as the integers and the operation symbols +, -, *, / , % etc) because the calculator page is completely dynamic. The symbols are queried from the DB via id number. Right now I'm using a switch statement to handle everything but that switch statement is huge, it's almost 24 cases long and I want to compact this into an array for functionality purposes and speed. I've tried everything from eval() (which I do not want to use unless I absolutely have no other choice), concatenating the variables together and while this shows what equation is supposed to take place, it doesn't perform the actual equation because the operator symbol is just a string. I'm truly stuck and have been researching a solution almost two days. here's a clip of the switch and case depends upon which operation the user wishes to perform: switch($Calc){ case 2: $Total = $firstvalue + $secondvalue; break; case 3: $Total = $firstvalue - $secondvalue; break; case 4: $Total = $firstvalue * $secondvalue; break; case 5: $Total = $firstvalue / $secondvalue; break; case 6: $Total = $firstvalue % $secondvalue; break; default: echo 'No modifier selected!'; break; } I said earlier, this is just a snippet of the switch, the original is 24 cases long and I really don't want to have to use a switch if I can get away with it but I'm not sure how to do this so I can use an array or a nested array loop. Thank you for your help. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 2, 2012 Share Posted January 2, 2012 I think eval() may be the 'simplest' solution. eval ('$total = $firstvalue' . $symbol . '$secondvalue ;'); Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 2, 2012 Share Posted January 2, 2012 I think eval() may be the 'simplest' solution. eval ('$total = $firstvalue' . $symbol . '$secondvalue ;'); Agreed. There are times when eval() is a good choice. Calculators are one of those few times. Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 2, 2012 Author Share Posted January 2, 2012 Thanks MasterACE14 and scootash for your replies, it's appreciated. While it may be the 'simpliest' method, is it the 'safest'? That's what I'm considered about regarding using it. The data is stored and dynamically built into selection boxes but still, those values can be changed using certain browser components now-a-days to include just about anything. or am I just being unnecessarily paranoid? Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 2, 2012 Author Share Posted January 2, 2012 Disregard my last question. I realize it's dangerous, I'll just have to sanitize the data thoroughly. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 2, 2012 Author Share Posted January 2, 2012 Sorry to bother you guys again, but I can't get this to function properly and I can't see where the error is coming from. function ModifyCalc($sum, $ModType, $ModValue, $OptModType, $OptModValue){ $NewSum = 0; if ($ModType >= 2 && $OptModType == 1){ eval ('$NewSum = $sum' . $ModType . '$ModValue ;'); return $NewSum; } To summarize, the user inputs the data in the calculator, hit submit and a function is called on that page, which you can see above, passing the data to the function parameters as you see above. The '$Opt' variables are only used (but do have default values) if the user wishes to further modify their calculation. When I echo $NewSum, I receive 0 which means to me Eval return NULL which doesn't make sense. When I test eval() by itself with constant values on a test page, it works but once I include it in a function or if() it fails, what am I doing wrong? I've checked the manual and I don't see anything wrong with my code. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2012 Share Posted January 2, 2012 What's the error, and how are you calling the function? Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 2, 2012 Author Share Posted January 2, 2012 Sorry, I thought I posted the error, my bad. It is: Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\inc\functions_inc.php(110) : eval()'d code on line 1 and I'm calling the function like so: if (($ModType != 1 && $ModValue != 0) || ($OptModType != 1 && $OptModValue != 0)) { echo ModifyCalc($sum, $ModType, $ModValue, $OptModType, $OptModValue); } Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 2, 2012 Share Posted January 2, 2012 are you missing the second curly brace in here? function ModifyCalc($sum, $ModType, $ModValue, $OptModType, $OptModValue){ $NewSum = 0; if ($ModType >= 2 && $OptModType == 1){ eval ('$NewSum = $sum' . $ModType . '$ModValue ;'); return $NewSum; } } // to close the function Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 2, 2012 Author Share Posted January 2, 2012 Sorry, I must have quick copied and didn't get the whole thing. No, it's there thank goodness Quote Link to comment Share on other sites More sharing options...
kicken Posted January 2, 2012 Share Posted January 2, 2012 Your if statement implies that $ModType is a number (2,3,4,..) however in your eval your using it as if it were a symbol (+,-,*,...) Quote Link to comment Share on other sites More sharing options...
wolfcry Posted January 2, 2012 Author Share Posted January 2, 2012 Hey Kicken, Ah, yes I was. I'm pulling the symbols from a db table using a reference id so I could use integer triggers in the actual script rather than hard coding everything. I guess what I'll do now is store the symbols in an array and pull them out based on index value, which of course will be the reference id. I'll just have to change the code up a bit. Thanks everyone for your help, I really appreciated it and all is working as it should now. Quote Link to comment 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.