Jump to content
MakeWebGames

Declaring 49 variables efficently o.o


Recommended Posts

Posted

Hey i started working on a game from scratch and not using an engine anymore, and im working on my registration page and for my game i have 12 races and its 6 stats and a location that different for each race. So the way i planned to do it was like for example:

if ($_POST['race']='Human'') {
$location=1;
$stat1=15;
$stat2=20;
}

if ($_POST['race']='Dog'') {
$location=2;
$stat1=30;
$stat2=25;
}

 

So it would be 12 different IF statements declaring 7 variables each o.o is there a more efficient and clean way of doing this?

Posted

You could use an array ie


$array = array("class" => array("location" => 1, "stat1" => 10, "stat2" => 30), "class2" => array("location" => 1, "stat1" => 10, "stat2" => 30));

Echo $array[$_POST['class']]['stat1'];

Posted

Well, when I read the title I was like "what? That little?".

Before you even do anything, PHP already populates around 100 variables, and about five times as many constants.

But anyway..

If you want efficiency, the easiest way would be(as Dayo hinted) an associative array.

Depending on your situation, this could be a few levels deep.

Another way to do it is with an ORM interface, but this requires a little bit more work(depending on your framework).

The benefit that ORM gives you, is that you have an object that can be executed, manipulated and more.

Unlike an array, your output can be dynamic, and callbacks can be set up to perform asynchronous(well almost, it's PHP) actions.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...