Keep track of multiple variables with one switch

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
SerbanSpire
Posts: 19
Joined: Fri Dec 14, 2012 11:23 am

Keep track of multiple variables with one switch

Post 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 :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Keep track of multiple variables with one switch

Post 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.
The indelible lord of tl;dr
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Keep track of multiple variables with one switch

Post 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);

?>
The indelible lord of tl;dr
SerbanSpire
Posts: 19
Joined: Fri Dec 14, 2012 11:23 am

Re: Keep track of multiple variables with one switch

Post by SerbanSpire »

Did what you suggested....it's perfect >:D<
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Keep track of multiple variables with one switch

Post 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 ;)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”