AbydosGater Posted September 8, 2007 Share Posted September 8, 2007 Hi, Ive been doing some work on a basic game.. and it is all working out well. Except for this bit. Every player gets a planet.. and on these planets the can have farmers and farms. So lets say $farms is the number of farms and $farmers is the number of farmers. If you want to use X as farms and Y as farmers thats ok . Each farm can man 1 and only 1 farmer.. so every farm gets one farmer.. How would i go about working out how many farms have a farmer and how many farms dont have farmers? Wow i just realized how confusing that all sounds. Just for example .. lets say there are 10 farms.. and 8 farmers.. straight off we know that there will be 8 manned farms and 2 unmanned farms. But how could i go about doing this inside a script with different numbers for each user? Thank you Andy Quote Link to comment https://forums.phpfreaks.com/topic/68485-solved-prob-simple-how-to-work-out-farmers-on-farms/ Share on other sites More sharing options...
benjaminbeazy Posted September 8, 2007 Share Posted September 8, 2007 this can be done many ways depending on your application and preference, here are 2 ideas. the first generates 2 global variables $manned, and $unmanned set to their respective values after calling the function. the second echos the numbers. the third returns an array containing "manned" and "unmanned" and their respective integers. this should give you some ideas. function farmers_calculate($farms, $farmers){ global $unmanned, $manned; $unmanned = $farms - $farmers; $manned = $farms - $unmanned; } farmers_calculate(10,; echo "$unmanned<br>"; echo "$manned<br>"; function farmers_calculate($farms, $farmers){ $unmanned = $farms - $farmers; $manned = $farms - $unmanned; echo "There are $manned manned farm(s) and $unmanned unmanned farm(s)"; } farmers_calculate(10,; function farmers_calculate($farms, $farmers){ $numbers['unmanned'] = $farms - $farmers; $numbers['manned'] = $farms - $numbers['unmanned']; return $numbers; } $numbers = farmers_calculate(10,; Quote Link to comment https://forums.phpfreaks.com/topic/68485-solved-prob-simple-how-to-work-out-farmers-on-farms/#findComment-344277 Share on other sites More sharing options...
AbydosGater Posted September 8, 2007 Author Share Posted September 8, 2007 Great. Thank you soooo much. Works like a charm.. all methods Thank you very much Andy Quote Link to comment https://forums.phpfreaks.com/topic/68485-solved-prob-simple-how-to-work-out-farmers-on-farms/#findComment-344302 Share on other sites More sharing options...
Daniel0 Posted September 10, 2007 Share Posted September 10, 2007 function farmers_calculate($farms, $farmers){ global $unmanned, $manned; $unmanned = $farms - $farmers; $manned = $farms - $unmanned; } farmers_calculate(10,; echo "$unmanned<br>"; echo "$manned<br>"; You shouldn't use globals like that. It'll make your code less reusable. I'd probably go with the last example. Quote Link to comment https://forums.phpfreaks.com/topic/68485-solved-prob-simple-how-to-work-out-farmers-on-farms/#findComment-345244 Share on other sites More sharing options...
AbydosGater Posted September 11, 2007 Author Share Posted September 11, 2007 Yes. I used all of these methods to come up with a good mix. And yes Daniel is right. You shouldnt use globals like that. Just return both manned and unmanned as an array and split them . But thank you very much. Andy Quote Link to comment https://forums.phpfreaks.com/topic/68485-solved-prob-simple-how-to-work-out-farmers-on-farms/#findComment-345826 Share on other sites More sharing options...
corbin Posted September 11, 2007 Share Posted September 11, 2007 function farmers_calculate($farms, $farmers){ global $unmanned, $manned; $unmanned = $farms - $farmers; $manned = $farms - $unmanned; } farmers_calculate(10,; echo "$unmanned<br>"; echo "$manned<br>"; You shouldn't use globals like that. It'll make your code less reusable. I'd probably go with the last example. A better alternative to globals could be variables passed by reference.... Quote Link to comment https://forums.phpfreaks.com/topic/68485-solved-prob-simple-how-to-work-out-farmers-on-farms/#findComment-346378 Share on other sites More sharing options...
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.