Page 1 of 1

Keep track of multiple variables with one switch

Posted: Thu Dec 20, 2012 6:57 pm
by SerbanSpire
Hello again :). Let me first describe you my goal.
I have a ship, frigate, which can be equipped with 3 different weapons. The weapons which can go in these 3 slots(let's call them inputs) are Torpedo, Laser, Rail Gun and Missile Launcher (for example).

I made 3 variables which are stored with the weapons i requested.
$weapon1 ='abc'; if (isset($_POST['weapon1'])) {$weapon1 = $_POST['weapon1'];}
$weapon2 ='abc'; if (isset($_POST['weapon2'])) {$weapon2 = $_POST['weapon2'];}
$weapon3 ='abc'; if (isset($_POST['weapon3'])) {$weapon3 = $_POST['weapon3'];}

I really don't want my code to look something like this:

Code: Select all

switch ($weapon1) {
case : 'Torpedo'
//code
break;
case : 'Laser'
//code
break;
case: 'Rail Gun'
//code
break;
case: Missile Launcher'
//code
break;
}

switch ($weapon2) {
case : 'Torpedo'
//code
break;
case : 'Laser'
//code
break;
case: 'Rail Gun'
//code
break;
case: Missile Launcher'
//code
break;
}

switch ($weapon3) {
case : 'Torpedo'
//code
break;
case : 'Laser'
//code
break;
case: 'Rail Gun'
//code
break;
case: Missile Launcher'
//code
break;
}
Guess already where i am trying to get here?:D
How can i make one switch that can help me do the same thing as above? What happens when i'll have a ship that can have 10 different weapons equipped? Pfff the code will go for miles :)). The //code actually updates my database with the different bonuses each weapon offers to my frigate, so it's irrelevant in this post. Hope i made the problem clear to you. Thanks in advance :)

Re: Keep track of multiple variables with one switch

Posted: Thu Dec 20, 2012 7:55 pm
by Jackolantern
Ok, let me try to get this straight in my head. You are going to have multiple $weapon variables, and they are each going to be set with a weapon in them. For example, say you have 10 weapons, and then you want to fire them, or do whatever with them. Your problem is setting up a SWITCH statement for each variable and having to check if each variable holds each weapon, right? If so, then an array would greatly help you. You could just process the array with a FOR loop, and you would only have to write the SWITCH statement once in the FOR loop, and that one SWITCH would handle each element of the array in turn. But you are probably going to have to have at least one SWITCH statement or set of IF statements. An array will keep you from having to duplicate it, though.

Re: Keep track of multiple variables with one switch

Posted: Thu Dec 20, 2012 8:03 pm
by Jackolantern
You can dynamically add more elements to the array as needed. Here is an example of adding elements onto the end of an array no matter how long it gets:

Code: Select all

<?php

$arr = array("hello");

$arr[count($arr)] = "world";
$arr[count($arr)] = "for";
$arr[count($arr)] = "now";

var_dump($arr);

?>

Re: Keep track of multiple variables with one switch

Posted: Fri Dec 21, 2012 12:24 am
by SerbanSpire
Did what you suggested....it's perfect >:D<

Re: Keep track of multiple variables with one switch

Posted: Fri Dec 21, 2012 12:30 am
by Jackolantern
Awesome! Arrays are indispensable in programming, and even the most trivial of programs that do some kind of real work will need them. The dynamic nature of arrays in PHP and Javascript make them even more awesome. They will become your best friend ;)