Whats the best way to fix this switch statement?

Hello all,

I have an issue I’m trying to resolve but am starting to pull my hair out and I can’t find anything pertaining to what I’m trying to do on the net. I’m hoping someone can point me in the right direction because I’m starting to go bald lol.

What I have is a form that allows a user to input a bunch of data, from the usual to numerical data that can be modified in a variety of ways, which I will get to in a second. This numerical value is kinda like their own secret key for unlocking various sections of the site only privy to members. It also has a very specific use that I can’t really get into.

Now, what I’ve been doing is using a switch statement nested in a function like so:


function OptModifyRoll($sum, $ModType, $ModValue, $OptModType, $OptModValue){
	$NewSum = 0;
	
	if ($ModType >= 2 && $OptModType == 1){
	
	switch($ModType){
	case 2:

	$NewSum = ($sum + $ModValue) + $OptModValue;

	break;
	case 3:

	$NewSum = ($sum - $ModValue) - $OptModValue;

	break;
	case 4:

	$NewSum = ($sum * $ModValue) * $OptModValue;

	break;
	case 5:

	$NewSum = ($sum / $ModValue) / $OptModValue;

	break;
	default: echo 'No modifier selected!';
	break;
	}
	
	return $NewSum;
 }



On my page, it’s a lot tidier, but for some reason, I can’t get this to format properly. Now, what’s supposed to happen is the user inputs the data that populates the associated variables which are then passed to the function. It wasn’t until today that I realized I had a huge hole and need to rewrite this but I’m having difficulty coming up with something that works.

The variables stand for the following:
$ModType = the type of operator the user wishes to use (+, -, *, /)
$ModValue = What value the user wishes to modify their original numerical value by, this can be from 1 to 20)

Both $OptModType and Value are exactly the same thing as above, but just modifies the user’s number regardless if it was modified or not. Something like this in equation format ((UserInput, ModType, ModVal) OptType, OptVal) or ((1 + 2) * 3) if that’s easier to understand.

The reason I have a huge hole is because of the following:

  1. The math is static and based on IF the user actually selects the same ModType and OptModType. But nothing is there to render if the user selects say * / -. Which means I’d have to create a 16 case switch statement and IMHO, that’s quite large for a switch.

  2. If the user leaves any of the modifier options blank (meaning they don’t actually select a ModType or OptModValue, the value is defaulted to 0, which will make the values modified by multiplying or dividing zero and that’s not what I want, so now I’ll have to add more If statements to fix that. I’m sure there’s more but those are the two biggest faults I can see.

Now, I know an array should take care of this, but too be honest, I’m lost on how I’d actually use an array to make this completely dynamic.

Thank you for all help in advance and I hope the above makes sense and if not, I’ll try to explain it a bit more clearly.

I forgot to mention that I’ve stored the mathematical operators in a database table for dynamic form purposes if that helps any.

Lol, hey omgdekdee and thank you.

Actually I’ve figured it out after countless hours of researching, reading and re-reading my PHP books and pulling what hair I had left out of my skull :smiley:

I’m glad you managed to solve your problem. Would you care to post the solution here, so this thread might be helpful to others that encounter the same kind of problem?