Page 1 of 1
Can somebody please take a look at this snippet of code?
Posted: Fri Aug 11, 2006 9:33 am
by impulse()
Pimptastic | Please use 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
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]
Posted: Fri Aug 11, 2006 9:42 am
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>
?>
Posted: Fri Aug 11, 2006 9:50 am
by impulse()
feyd | Please use 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
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]
Posted: Fri Aug 11, 2006 10:07 am
by impulse()
feyd | Please use 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
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]
Posted: Fri Aug 11, 2006 10:15 am
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

Posted: Fri Aug 11, 2006 10:21 am
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.
Posted: Fri Aug 11, 2006 10:23 am
by feyd
impluse, here's how the tags work: [syntax=php]..[/syntax] or [syntax=php]..[/syntax] not [syntax=php][/syntax][syntax=php]..[/syntax].
Posted: Fri Aug 11, 2006 10:24 am
by impulse()
Yeah, I noticed someone else was editing my posts at the same time as me
Cheers dude