Shockhazard30 Posted December 11, 2009 Share Posted December 11, 2009 I am unable to get $threeKind to output outside of the countDice function my code may not look the best I am new to this stuff. If anyone can tell me why I am having this problem I would appreciate it. rollDice(); countDice(); function rollDice(){ global $die, $keepIt, $threeKind; print $threeKind; print "<table Height = 338>\n<tr>\n<td width = 250 background = 'http://127.0.0.1/img/fdice2.jpg'>\n</td>\n<td valign = top><center>\n<table Border = 1>\n<td>\n<tr><br><br><br>"; for ($i = 0; $i < 5; $i++) { if ($keepIt[$i] == "") { $die[$i] = rand(1, 6); } else { $die[$i] = $keepIt[$i]; } // end if $theFile = "http://127.0.0.1/img/die" . $die[$i] . ".jpg"; // print out dice images print <<<HERE <td height = 105 valign = "top"> <img src = "$theFile" height = 80 width = 80> <br><input type = "checkbox" name = "keepIt[$i]" value = $die[$i]> </td> HERE; }// end for loop //print out submit button and end of table print <<<HERE </tr></td> <tr> <td colspan = "5" height = 15> <center> <input type = "submit" value = "Roll Again"> </center> </td> </tr> </table> HERE; } // end rollDice function countDice(){ global $die, $threeKind; $numVals = array(6); for ($theVal = 1; $theVal <=6; $theVal++){ for ($dieNum = 0; $dieNum < 5; $dieNum++){ if ($die[$dieNum] == $theVal){ $numVals[$theVal]++; } // end if } // end dieNum for loop } // end theVal for loop // print out values for ($k= 1; $k <=6; $k++){ print "$k: $numVals[$k]<br>\n"; if ($numVals[$k] >=3){ $threeKind = "A three of a kind exists<br>\n"; } else { $threeKind = "\n"; } // end if print $threeKind; } // end for loop }//end countDice Link to comment https://forums.phpfreaks.com/topic/184714-getting-a-variable-to-ouput-outside-of-a-function/ Share on other sites More sharing options...
trq Posted December 11, 2009 Share Posted December 11, 2009 Trying to access global variables from within functions is a bad idea. You should pass data into functions as arguments, and return any data needed outside. Link to comment https://forums.phpfreaks.com/topic/184714-getting-a-variable-to-ouput-outside-of-a-function/#findComment-975130 Share on other sites More sharing options...
Shockhazard30 Posted December 11, 2009 Author Share Posted December 11, 2009 here is the whole chunk of code I am working on and I need to make this print out a checkbox if a three of a kind exists and It isn't happening. <?php print "<input type = 'hidden' name = 'gameTo' value = '$gameTo'>"; print "<input type = 'hidden' name = 'numPlayer' value = '$numPlayer'>"; rollDice(); function rollDice(){ global $die, $keepIt; print "<table Height = 338>\n<tr>\n<td width = 250 background = 'http://127.0.0.1/img/fdice2.jpg'>\n</td>\n<td valign = top><center>\n<table Border = 1>\n<td>\n<tr><br><br><br>"; for ($i = 0; $i < 5; $i++) { if ($keepIt[$i] == "") { $die[$i] = rand(1, 6); } else { $die[$i] = $keepIt[$i]; } // end if $theFile = "http://127.0.0.1/img/die" . $die[$i] . ".jpg"; // print out dice images print <<<HERE <td height = 105 valign = "top"> <img src = "$theFile" height = 80 width = 80> <br> HERE; if (($die[$i] == 1) || ($die[$i] == 5) || ($die[$i] == $threeKind)) { print "<input type = 'checkbox' name = 'keepIt[$i]' value = $die[$i]></td>"; } else { print "</td>"; } // end if }// end for loop $numVals = array(6); for ($theVal = 1; $theVal <=6; $theVal++) { for ($dieNum = 0; $dieNum < 5; $dieNum++) { if ($die[$dieNum] == $theVal) { $numVals[$theVal]++; } // end if } // end dieNum for loop } // end theVal for loop // print out values for ($k= 1; $k <=6; $k++) { print "$k: $numVals[$k]<br>\n"; if ($numVals[$k] >=3) { $threeKind = "$k"; } else { $threeKind = "\n"; } // end if print $threeKind; } // end for loop //print out submit button and end of table print <<<HERE </tr></td> <tr> <td colspan = "5" height = 15> <center> <input type = "submit" value = "Roll Again"> </center> </td> </tr> </table> HERE; } // end rollDice print "output all possible variables <br>"; print "<table border = 1>\n"; for ($j = 1; $j <= $numPlayer; $j++) { print <<<HERE <input type = "hidden" name = "player[$j]" value = "$player[$j]"> <tr>\n<td>\n$player[$j]</td><td>Score will go here</td> </tr>\n HERE; } // end for print "</table>"; print "Game to : $gameTo<br>"; print "Number of players : $numPlayer<br>\n"; print $threeKind; ?> I am not sure if I understand what you meant in your response. Link to comment https://forums.phpfreaks.com/topic/184714-getting-a-variable-to-ouput-outside-of-a-function/#findComment-975173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.