ID: 48904 User updated by: 7willows at dsl dot pipex dot com Reported By: 7willows at dsl dot pipex dot com -Status: Feedback +Status: Open Bug Type: Math related Operating System: SunOS ts10 5.11 snv_115 i86pc PHP Version: 5.2.10 New Comment:
<?php function answerlength($maxnumberfacts) { $maxvalue=$maxnumberfacts * $maxnumberfacts ; $length=strlen($maxvalue); return $length; } function setquestioncount($maxnumberfacts) { switch($maxnumberfacts) { case 2: $count=10; break; case 3: $count=20; break; case 4: $count=20; break; case 5: $count=20; break; case 6: $count=20; break; case 7: $count=20; break; case 8: $count=20; break; case 9: $count=20; break; case 10: $count=20; break; case 11: $count=20; break; case 12: $count=20; break; case 13: $count=20; break; case 14: $count=20; break; case 15: $count=20; break; case 16: $count=20; break; case 17: $count=20; break; case 18: $count=20; break; case 19: $count=20; break; case 20: $count=20; break; } return $count; } function multiplicationanddivision() { $operator=rand(0,1); if ($operator) { return '/'; } else { return '*'; } } function setnumberfacttest($count,$maxnumberfacts) { global $numfacttest, $answer, $operator, $randval; for ($a = 0 ; $a < $count ; $a++) { $numfacttest[$a]=rand(0,$maxnumberfacts); $randval[$a]=rand(0,$numfacttest[$a]); $operator[$a]='x'; switch($operator[$a]) { case 'x': $answer[$a]=$numfacttest[$a] * $randval[$a]; break; case '/': $answer[$a]=$numfacttest[$a] * $randval[$a]; $tmp=$numfacttest[$a]; $numfacttest[$a]=$answer[$a]; $answer[$a]=$tmp; break; case '+': $answer[$a]=$numfacttest[$a] - $randval[$a]; break; case '-': $answer[$a]=$numfacttest[$a] - $randval[$a]; break; } } } if (isset($_POST['mainmenu'])) { header('Location: /education.php'); exit(); } if (isset($_POST['changerange'])) { header('Location: multiplicationwelcome.php'); exit(); } if (isset($_POST['maxfacts'])) { $maxnumberfacts=$_POST['maxfacts']; $numfacttest[0]=0; $answer[0]=0; $operator[0]="x"; $randval[0]=0; $questionlength=answerlength($maxnumberfacts); $count=setquestioncount($maxnumberfacts); setnumberfacttest($count,$maxnumberfacts); include("edupageheader.php"); echo '<body>'; echo '<h1>Multiplication Number Facts to ',$maxnumberfacts,'</h1>'; echo 'Please answer the following questions...'; echo '<br /><br />'; echo '<form method="POST" action="multiplicationtables.php" id="mathsform" />'; for ($a = 0; $a < $count; $a++) { switch($operator[$a]) { case 'x': echo '<input type="text" name="val1ue',$a,'" value="',$numfacttest[$a],'" readonly="readonly" maxlength="',$questionlength,'" size="',$questionlength,'" />'; echo '<input type="text" name="operator',$a,'" value="',$operator[$a],'" readonly="readonly" maxlength="',$questionlength,'" size="',$questionlength,'" />'; echo '<input type="text" name="val2ue',$a,'" value="',$randval[$a],'" readonly="readonly" maxlength="',$questionlength,'" size="',$questionlength,'" />'; echo ' = '; echo '<input type="text" name="useranswer',$a,'" value="" maxlength="',$questionlength,'"size="',$questionlength,'" />'; echo '<input type="hidden" name="answer',$a,'" value="',$answer[$a],'"/>'; echo "<br />"; break; case '/': echo '<input type="text" name="val1ue',$a,'" value="',$numfacttest[$a],'" readonly="readonly" maxlength="',$questionlength,'"size="',$questionlength,'" />'; echo '<input type="text" name="operator',$a,'" value="',$operator[$a],'" readonly="readonly" maxlength="',$questionlength,'" size="',$questionlength,'" />'; echo '<input type="text" name="val2ue',$a,'" value="',$randval[$a],'" readonly="readonly" maxlength="',$questionlength,'" size="',$questionlength,'" />'; echo ' = '; echo '<input type="text" name="useranswer',$a,'" value="" maxlength="',$questionlength,'"size="',$questionlength,'" />'; echo '<input type="hidden" name="answer',$a,'" value="',$answer[$a],'"/>'; echo "<br />"; break; } } echo '<br />'; echo '<input type="submit" name="checkanswers" value="check answers" />'; echo '<input type="submit" name="changerange" value="change number facts" />'; echo '<input type="submit" name="mainmenu" value="main menu" />'; echo '<input type="hidden" name="questions" value="',$count,'" />'; echo '<input type="hidden" name="maxsum" value="',$maxnumberfacts,'" />'; echo '</form>'; echo '</body>'; echo '</html>'; exit(); } if (isset($_POST['checkanswers'])) { $count=$_POST['questions']; $maxnumberfacts=$_POST['maxsum']; include("edupageheader.php"); echo '<body>'; echo '<h1>Multiplication Number Facts to ',$maxnumberfacts.'</h1>'; for ($a=0 ; $a < $count ; $a++) { $part1="val1ue" . $a ; $part2="val2ue" . $a ; $part3="useranswer" . $a ; $part4="answer" . $a ; $part5="operator" . $a ; $value_a=$_POST[$part1]; $value_b=$_POST[$part2]; $value_c=$_POST[$part3]; $value_d=$_POST[$part4]; $value_e=$_POST[$part5]; switch($value_e) { case 'x': echo "<tr> <td>",$value_a,"</td><td> ",$value_e," </td><td>",$value_b,"</td><td> = </td><td>",$value_c,"</td>" ; break; case '/': echo "<tr> <td>",$value_a,"</td><td> ",$value_e," </td><td>",$value_b,"</td><td> = </td><td>",$value_c,"</td>" ; break; } if ($value_c == $value_d) { echo "<td> correct! </td></tr"; } else { echo "<td> wrong! </td></tr>"; } echo "<br />"; echo "<br />"; } echo '<form method="POST" action="multiplicationtables.php" id="mathsform" />'; echo '<input type="submit" name="morequestions" value="more questions" />'; echo '<input type="submit" name="changerange" value="change number facts" />'; echo '<input type="submit" name="mainmenu" value="main menu" />'; echo '<input type="hidden" name="maxfacts" value="',$maxnumberfacts,'"/>'; echo '</form>'; echo '</body>'; echo '</html>'; } exit(); ?> Previous Comments: ------------------------------------------------------------------------ [2009-07-13 19:02:25] j...@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2009-07-13 13:51:32] 7willows at dsl dot pipex dot com Description: ------------ I have a function which is used to assign values into a number of global arrays. Two of the arrays are assigned values that are generated using the rand() function. A third array holds the result of the multiplication. When both first two arrays are assigned a value of 0 (zero), the result of the multiplication is 1 and not 0. This information is then entered into a html form and presented to the user and they are asked to enter the results of $numfacttest[$a] / $randval[$a] = useranswer The useranswer is then compared to $answer[$a] which is already calculated. The problem is that 0 * 0 is returning the answer 1 sometimes. Reproduce code: --------------- function setnumberfacttest($count,$maxnumberfacts) { global $numfacttest, $answer, $operator, $randval; for ($a=0 ; $a < $count ; $a++) { $operator[a]=chr(247); $bit1=rand(0,$maxnumberfacts); $bit2=rand(0,$maxnumberfacts); $numfacttest[$a]=$bit1 * $bit2; $randval[$a]=$bit2; $answer[$a]=$bit1; } } Expected result: ---------------- Actual Results with hidden value displayed 2 / 1 = <useranswer> 2 0 / 0 = <useranswer> 0 0 / 0 = <useranswer> 0 2 / 2 = <useranswer> 1 4 / 2 = <useranswer> 2 1 / 1 = <useranswer> 1 0 / 0 = <useranswer> 0 0 / 0 = <useranswer> 0 0 / 0 = <useranswer> 0 1 / 1 = <useranswer> 1 Actual result: -------------- Actual Results with calculated hidden value displayed 2 / 1 = <useranswer> 2 0 / 0 = <useranswer> 1 0 / 0 = <useranswer> 0 2 / 2 = <useranswer> 1 4 / 2 = <useranswer> 2 1 / 1 = <useranswer> 1 0 / 0 = <useranswer> 1 0 / 0 = <useranswer> 0 0 / 0 = <useranswer> 2 1 / 1 = <useranswer> 1 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=48904&edit=1