corbo950 Posted May 7, 2008 Share Posted May 7, 2008 This is a script designed to run a list of arithmetic functions. It is for a math game where one rolls 3 dice to get a number then roll them again to get 3 numbers that using roots, multiplication, addition, subtraction, division, and exponents. When I input the numbers I get back a blank page. Thanks to all who help. <?php $functions = array( "$a+$b+$c", "$a+$b-$c", "$a+$b*$c", "$a+$b/$c", "$a+$b^(1/$c)", "$a+$b^$c", "$a+^($b+$c)", "$a+^($b-$c)", "$a+^($a*$c)", "$a+^($b/$c)", "$a+^($b^(1/$c))", "$a+^($b^$c)", "$a+^(1/($b+$c))", "$a+^(1/($b-$c))", "$a+^(1/($a*$c))", "$a+^(1/($b/$c))", "$a+^(1/($b^(1/$c)))", "$a+^(1/($a^$c))"); if(!$_POST['A']) { ?> <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> Target:<input name="Target" type="text" size="20" maxlength="4"> Number 1:<input name="A" type="text" size="20" maxlength="2"> Number 2:<input name="B" type="text" size="20" maxlength="2"> Number 3:<input name="C" type="text" size="20" maxlength="2"> <input type="submit" name="Send" value="submit"> </form> <?php } else { $target = $_POST['Target']; $a = $_POST['A']; $b = $_POST['B']; $c = $_POST['C']; $nums = array(brute($a, $b, $c)); array_push ($num, brute($a, $c, $b)); array_push ($num, brute($b, $a, $c)); array_push ($num, brute($b, $c, $a)); array_push ($num, brute($c, $a, $b)); array_push ($num, brute($c, $b, $a)); $results = compare($nums, $target); while (list($pos, $result) = each($results)) { echo($result['result']); echo($result['function']); } } function brute($a, $b, $c) { $array = array(); while(list($pos, $function) = each($functions)) { $array2 = array ( "result"=>sprintf($function), "function"=>$function ); array_push($array, $array2); } return($array); } function compare($arrays, $target) { while(list($pos, $array)= each($arrays)) { $diffs = array(); array_push($diffs, $target - $array['result']); } $mins = min(diffs); while (list($pos, $min) = each( $mins)) { $results = array(); while (list($pos, $diff)= each($diffs)) { if ($min = $diff['result']) { array_push($results, $diff); } } } return ($results); } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 7, 2008 Share Posted May 7, 2008 Im afraid there are an awful lot of errors in your script. If you place this line: error_reporting(E_ALL); At the top of your script, you'll see what i mean. The first lot are due to $a,$b and $c being undefined - you should move the creation of the $functions array to after they have been defined on these lines: $a = $_POST['A']; $b = $_POST['B']; $c = $_POST['C']; You also make use of the $functions variable inside your brute() function, but it is again undefined. If you're not quite sure why, you'll need to read up on variable scope. You should be passing that array to your function. $num also appears to be undefined on these lines: array_push ($num, brute($a, $c, $b));//i guess it should be $nums? There are other errors too, but im not entirely sure how this is supposed to work, so you'll need to fix those for yourself. On a side note, the caret(^) character cannot be used to raise two numbers to a power. You'll need PHP's pow() function for that. Quote Link to comment Share on other sites More sharing options...
corbo950 Posted May 7, 2008 Author Share Posted May 7, 2008 Ya the whole reason i had to post here is i cant get error reporting to to work so i cant debug it and the variables shouldnt be undefined because in the array they are nothing but strings Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 7, 2008 Share Posted May 7, 2008 Ya the whole reason i had to post here is i cant get error reporting to to work so i cant debug it and the variables shouldnt be undefined because in the array they are nothing but strings Why cant you get error reporting to work? Does your host not allow it? You could always install PHP on your computer and test locally. And yes, they are strings. However, if you place your string inside double quotes, then any variables will be evaluated. That is: <?php $foo = 'bar'; echo "$foo";//echos the text bar echo '$foo';//echos the text $foo ?> They do therefore need to be defined. Quote Link to comment Share on other sites More sharing options...
corbo950 Posted May 7, 2008 Author Share Posted May 7, 2008 ya is on my local machine that it is having problems and this is a rush job dont have time to fix it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.