Daron Posted August 9, 2012 Posted August 9, 2012 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? Quote
Dayo Posted August 9, 2012 Posted August 9, 2012 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']; Quote
Dayo Posted August 9, 2012 Posted August 9, 2012 Please note that was untested an wrote on my phone so it may b wrong Quote
Spudinski Posted August 9, 2012 Posted August 9, 2012 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. Quote
Daron Posted August 9, 2012 Author Posted August 9, 2012 Thnx guys, the array thing worked out for me Quote
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.