Jump to content

Recommended Posts

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

 

 

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/273678-basic-questing-involving-array/
Share on other sites

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.