Can somebody please take a look at this snippet of code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Can somebody please take a look at this snippet of code?

Post by impulse() »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have recently started learning PHP and was wondering why this code isn't running?

Code: Select all

<?php

$dice = rand(1,6); //Roll//

echo "$dice"; //Display//

switch ($dice) {
      case 1: $counter[1] += 1; break;  //
      case 2: $counter[2] += 1; break;  //
      case 3: $counter[3] += 1; break;  //--- Counter for dice amount
      case 4: $counter[4] += 1; break;  //
      case 5: $counter[5] += 1; break;  //
      case 6: $counter[6] += 1; break;  //
    }

    print "$counter[1]"; 
    <br>
    echo "$counter[2]";
    <br>
    echo "$counter[3]";
    <br>
    echo "$counter[4]";
    <br>
    echo "$counter[5]";
    <br>
    echo "$counter[6]";
    <br>

?>

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

$counter is not defined before you start incrementing it. Arrays start numbering at zero, not 1.

Code: Select all

<?php

$counter = array(0,0,0,0,0,0);

$dice = rand(0,5); //Roll//

echo "$dice"; //Display//

switch ($dice) {
      case 1: $counter[0] += 1; break;  //
      case 2: $counter[1] += 1; break;  //
      case 3: $counter[2] += 1; break;  //--- Counter for dice amount
      case 4: $counter[3] += 1; break;  //
      case 5: $counter[4] += 1; break;  //
      case 6: $counter[5] += 1; break;  //
    }

    print "$counter[0]";
    <br>
    echo "$counter[1]";
    <br>
    echo "$counter[2]";
    <br>
    echo "$counter[3]";
    <br>
    echo "$counter[4]";
    <br>
    echo "$counter[5]";
    <br>

?>
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Do you believe this should work as I've created a loop to set a value of 0 to all variables in the $counter array.

Code: Select all

<?php

for ($i = 0; $i <=5; $i++) {  //** IGNORE THIS. I'VE JUST REALISED WHAT WILL HAPPEN ** 
   $counter[$i] = 0;
  }

$dice = rand(0,5);

echo "$dice";


switch ($dice) {
      case 0: $counter[0] += 1; break;
      case 1: $counter[1] += 1; break;
      case 2: $counter[2] += 1; break;
      case 3: $counter[3] += 1; break;
      case 4: $counter[4] += 1; break;
      case 5: $counter[5] += 1; break;
    }


    print $counter[0];
    <br>
    print $counter[1];
    <br>
    print $counter[2];
    <br>
    print $counter[3];
    <br>
    print $counter[4];
    <br>
    print $counter[5];
    <br>

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by impulse() on Fri Aug 11, 2006 10:15 am, edited 3 times in total.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Right, I have working code now but the code never keeps track of the variables throughout each run of the program. Is this due to HTTP being stateless?
This is the working code I have

Code: Select all

<?php

for ($i = 1; $i <=6; $i++) {
   $counter[$i] = 0;
  }

$dice = rand(1,6);

echo "<br>Dice Roll is:  $dice<br><br>";


switch ($dice) {
      case 1: $counter[1] += 1; break;
      case 2: $counter[2] += 1; break;
      case 3: $counter[3] += 1; break;
      case 4: $counter[4] += 1; break;
      case 5: $counter[5] += 1; break;
      case 6: $counter[6] += 1; break;
    }

    for ($a = 1; $a <= 6; $a++) {
       $saved[$a] += $counter[$a];
      }

    echo "1s: $saved[1]<br>";
    echo "2s: $saved[2]<br>";
    echo "3s: $saved[3]<br>";
    echo "4s: $saved[4]<br>";
    echo "5s: $saved[5]<br>";
    echo "6s: $saved[6]<br>";

?>
Ste


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by impulse() on Fri Aug 11, 2006 10:17 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need to use sessions to track the counting over multiple requests ;)

FYI... place error_reporting(E_ALL); at the top of your script.... you'll get notices sinc e$counter is never initialized using $counter = array(); .... I do hope your text book hasn't tought you that :)
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I'm still on the basics doing loops, variables and arrays. I just keep getting ideas while reading and go on a mad 'n trying to get my idea working :) I'll hit the book again for a while and stop jumping the gun.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

impluse, here's how the tags work: [syntax=php]..[/syntax] or [syntax=php]..[/syntax] not [syntax=php][/syntax][syntax=php]..[/syntax].
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Yeah, I noticed someone else was editing my posts at the same time as me :)

Cheers dude
Post Reply