cliftonbazaar Posted January 24, 2013 Share Posted January 24, 2013 I currently use long variable names so I know what each one is at a glance, I also use a two dimensional array. In the current piece of code I have tried to tighten it from for($i=1; $i<12; $i++) { $battingPlayer[$battingLineUp[$i]]['energy'] += mt_rand(10, floor($battingPlayer[$battingLineUp[$i]]['fitness']/10+10)); #If energy is low then player gets a bit of it back $bowlingPlayer[$bowlingLineUp[$i]]['energy'] += mt_rand(10, floor($bowlingPlayer[$bowlingLineUp[$i]]['fitness']/10+10)); #If energy is low then player gets a bit of it back if($battingPlayer[$battingLineUp[$i]]['energy'] > $battingPlayer[$battingLineUp[$i]]['fitness']) $battingPlayer[$battingLineUp[$i]]['energy'] = $battingPlayer[$battingLineUp[$i]]['fitness']; #Energy cannot be more than fitness if($bowlingPlayer[$bowlingLineUp[$i]]['energy'] > $bowlingPlayer[$bowlingLineUp[$i]]['fitness']) $bowlingPlayer[$bowlingLineUp[$i]]['energy'] = $bowlingPlayer[$bowlingLineUp[$i]]['fitness']; #Energy cannot be more than fitness } to this for($i=1; $i<12; $i++) { for($j=0; $j<2; $j++) { if($j) {$x = $battingPlayer[$battingLineUp[$i]];} else {$x = $bowlingPlayer[$bowlingLineUp[$i]];} echo "<br>old energy is ".$x['energy']." and now they have "; $x['energy'] -= mt_rand(10, floor($x['fitness']/10+10)); #If energy is low then player gets a bit of it back if($x['energy'] > $x['fitness']) $x['energy'] = $x['fitness']; #Energy cannot be more than fitness echo $x['energy']; } } While the echo statements show an increase in the number($x['energy']) the variable is not increased when it is used with the full variable name! ($battingPlayer[$battingLineUp[$i]]['energy']). I know one method is to use shorter variable names but I'm very happy with how they are Quote Link to comment https://forums.phpfreaks.com/topic/273567-helping-tighten-this-code-with-variables/ Share on other sites More sharing options...
requinix Posted January 24, 2013 Share Posted January 24, 2013 You can throw in references to get a shorter variable temporarily. for($i=1; $i<12; $i++) { $batter =& $battingPlayer[$battingLineUp[$i]]; $bowler =& $bowlingPlayer[$bowlingLineUp[$i]]; $batter['energy'] += mt_rand(10, floor($batter['fitness']/10+10)); #If energy is low then player gets a bit of it back $bowler['energy'] += mt_rand(10, floor($bowler['fitness']/10+10)); #If energy is low then player gets a bit of it back if($batter['energy'] > $batter['fitness']) $batter['energy'] = $batter['fitness']; #Energy cannot be more than fitness if($bowler['energy'] > $bowler['fitness']) $bowler['energy'] = $bowler['fitness']; #Energy cannot be more than fitness } unset($batter, $bowler); Note the unset(): references can be tricky and might bite you if you're not careful. Can also throw in a couple calls to min(). for($i=1; $i<12; $i++) { $batter =& $battingPlayer[$battingLineUp[$i]]; $bowler =& $bowlingPlayer[$bowlingLineUp[$i]]; $batter['energy'] += mt_rand(10, floor($batter['fitness']/10+10)); #If energy is low then player gets a bit of it back $bowler['energy'] += mt_rand(10, floor($bowler['fitness']/10+10)); #If energy is low then player gets a bit of it back $batter['energy'] = min($batter['energy'], $batter['fitness']); #Energy cannot be more than fitness $bowler['energy'] = min($bowler['energy'], $bowler['fitness']); #Energy cannot be more than fitness } unset($batter, $bowler); Can even combine the two pairs of $player['energy']= together, though the line gets pretty long. Quote Link to comment https://forums.phpfreaks.com/topic/273567-helping-tighten-this-code-with-variables/#findComment-1407843 Share on other sites More sharing options...
cliftonbazaar Posted January 24, 2013 Author Share Posted January 24, 2013 errrrr ... wow! On both counts Thanks for that, already updated my code and it works fantastic (and a lot easier to read!!) Quote Link to comment https://forums.phpfreaks.com/topic/273567-helping-tighten-this-code-with-variables/#findComment-1407845 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.