ParkingLot Posted January 26, 2013 Share Posted January 26, 2013 I have this set of code here out of my text book that i'm working with. I understand how all the code works except for one part that i'm not to sure what it's doing. The part that confuses me is in the if statement for example the $FaceNamesPlural[$Die1-1] the part that stumps me is the [$Die1-1] not really understanding what the -1 part is doing my only guess is that it's maybe pulling the element thats index is 1 in the array? Thanks for any feedback. <?php $FaceNamesSingular = array("one", "two", "three", "four", "five", "six"); $FaceNamesPlural = array("ones", "twos", "threes", "fours", "fives", "sixes"); function CheckForDoubles($Die1, $Die2) { global $FaceNamesSingular; global $FaceNamesPlural; if ($Die1 == $Die2) //Doubles echo "The roll was double ", $FaceNamesPlural[$Die1-1], ".<br>"; if ($Die1 != $Die2) //not Doubles echo "The toll was a ", $FaceNamesSingular[$Die1-1], " and a ", $FaceNamesSingular[$Die2-1], ".<br>"; } function DisplayScoreText($Score) { if ($Score == 2) echo "You rolled snake eyes!<br>"; if ($Score == 3) echo "You rolled a loose deuce!<br>"; if ($Score == 5) echo "You rolled a fever five!<br>"; if ($Score == 7) echo "You rolled a natural!<br>"; if ($Score == 9) echo "You rolled a nina!<br>"; if ($Score == 11) echo "You rolled a yo!<br>"; if ($Score == 12) echo "You rolled boxcars!<br>"; } $Dice = array(); $Dice[0] = rand(1,6); $Dice[1] = rand(1,6); $Score = $Dice[0] + $Dice[1]; echo "<p>"; echo "The total score for the roll was $Score. <br>"; checkForDoubles($Dice[0], $Dice[1]); DisplayScoreText($Score); echo "</p>"; ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 26, 2013 Share Posted January 26, 2013 It's compensating for the fact that array keys start at 0. Quote Link to comment Share on other sites More sharing options...
ParkingLot Posted January 27, 2013 Author Share Posted January 27, 2013 (edited) I understand that the array's start at [0][1][2] i'm just not sure what this part is exactly doing >> [$Die1-1] << Edited January 27, 2013 by ParkingLot Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 27, 2013 Share Posted January 27, 2013 What dice name is at key #2? Is that what you want to echo for rolling a 2? Quote Link to comment Share on other sites More sharing options...
kicken Posted January 27, 2013 Share Posted January 27, 2013 (edited) I understand that the array's start at [0][1][2] i'm just not sure what this part is exactly doing >> [$Die1-1] << It's just math. For example, if $Die1 is currently set to 6 then $Die1-1 is 6-1 which equals 5. Then it looks up index 5 in the given array, ie $FaceNamesPlural[5] which would be 'sixes' Edited January 27, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
ParkingLot Posted January 27, 2013 Author Share Posted January 27, 2013 It's just math. For example, if $Die1 is currently set to 6 then $Die1-1 is 6-1 which equals 5. Then it looks up index 5 in the given array, ie $FaceNamesPlural[5] which would be 'sixes' Thanks I understand it now! Quote Link to comment 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.