Jump to content


Photo

Basic questing involving array


  • Please log in to reply
5 replies to this topic

#1 ParkingLot

ParkingLot

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 26 January 2013 - 06:27 PM

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>";




?>

#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 26 January 2013 - 06:50 PM

It's compensating for the fact that array keys start at 0.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 ParkingLot

ParkingLot

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 26 January 2013 - 07:14 PM

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 by ParkingLot, 26 January 2013 - 07:15 PM.


#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 26 January 2013 - 07:23 PM

What dice name is at key #2? Is that what you want to echo for rolling a 2?
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#5 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,707 posts
  • LocationBonita, FL

Posted 26 January 2013 - 09:06 PM

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 by kicken, 26 January 2013 - 09:06 PM.

Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#6 ParkingLot

ParkingLot

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 27 January 2013 - 12:16 AM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com