Basic questing involving array
#1
Posted 26 January 2013 - 06:27 PM
<?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
Posted 26 January 2013 - 06:50 PM
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
Posted 26 January 2013 - 07:14 PM
Edited by ParkingLot, 26 January 2013 - 07:15 PM.
#4
Posted 26 January 2013 - 07:23 PM
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
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.
Did I help you out? Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7
#6
Posted 27 January 2013 - 12:16 AM
It's just math. For example, if $Die1 is currently set to 6 then
$Die1-1is6-1which 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











