Irresistable Posted January 3, 2010 Share Posted January 3, 2010 First, I know this is the wrong section, but being as it is very simple I thought to put it here. If I have. $first = '1' $method = '+' $second = '2' How would I change that into something that displays the sum together. I got something like.. $sum = $first . $method . $second . '='; If that is correct, and would echo out "1 + 2 =" Then, how do I set it so that it displays the answer.. So. $answer = ... I know it doesn't have to be set with variables, though for what I'm doing, it's needed to. Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/ Share on other sites More sharing options...
RussellReal Posted January 3, 2010 Share Posted January 3, 2010 eval.. $sum = eval("$first$method$second"); Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/#findComment-987718 Share on other sites More sharing options...
Irresistable Posted January 3, 2010 Author Share Posted January 3, 2010 What about getting the answer? Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/#findComment-987720 Share on other sites More sharing options...
RussellReal Posted January 3, 2010 Share Posted January 3, 2010 sorry.. use this: eval("\$sum = $first$method$second;"); my original code doesn't work ^^ edit Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/#findComment-987723 Share on other sites More sharing options...
Irresistable Posted January 3, 2010 Author Share Posted January 3, 2010 Thanks, my issue is probably not related to this, although it could be. The sum, doesn't change into an image, or lets say.. I don't know what the problem is, an image just isn't displayed. Here is my code. <?php session_start(); header ("Content-type: image/png"); // String Length $length = 1; // Primary Numbers - 0 To 9 $primary = '0123456789'; // Methods Add Or Subtract $method = '+-' // Secondary Numbers - 1 To 9 $secondary = '123456789'; // Reset strings $pri = ''; $met = ''; $sec = ''; // Primary Number - 0 To 9 $charslength = strlen($primary); for ($i = 0; $i < $length; $i++) $pri .= substr($primary, rand(0, $charslength - 1), 1); // Method - Add Or Subtract $charslength = strlen($method); for ($i = 0; $i < $length; $i++) $met .= substr($method, rand(0, $charslength - 1), 1); // Secondary Number - 1 To 9 $charslength = strlen($method); for ($i = 0; $i < $length; $i++) $sec .= substr($secondary, rand(0, $charslength - 1), 1); // Width (px) $width = 100; // Height (px) $height = 25; // Background Image $img_handle = imageCreateFromPNG("bg1.PNG"); // Change it into a sum eval("\$sum = $pri$met$sec;"); // Set the image string imagestring ($img_handle, 5, 15, 6, $sum, $color); //Creating the answer $answer = $pri . $met . $sec . '='; // The answer put in a session for validation $_SESSION['ckey'] = $answer; // Turn into an image imagepng ($img_handle); // Destroy the image imagedestroy ($img_handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/#findComment-987727 Share on other sites More sharing options...
Irresistable Posted January 3, 2010 Author Share Posted January 3, 2010 Edit: I fixed the problem. Also I updated it to // Change it into a sum eval("\$s = $pri $met $sec;"); $sum = $s.' = '; // Set the image string imagestring ($img_handle, 5, 15, 6, $sum, $color); Although, this only gets the $sec and = I believe. It doesn't get a full sum, just 1 number, and the equals. Why? Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/#findComment-987734 Share on other sites More sharing options...
Irresistable Posted January 3, 2010 Author Share Posted January 3, 2010 It should of been - eval("\$s = \"$pri $met $sec\";"); Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/#findComment-987747 Share on other sites More sharing options...
ignace Posted January 3, 2010 Share Posted January 3, 2010 You could also have done: $num1 = floatval($_POST['num1']); $num2 = floatval($_POST['num2']); $op = $_POST['op']; $total = 0; switch ($op) { case '+': $total = $num1 + $num2; break; } print $total; Quote Link to comment https://forums.phpfreaks.com/topic/187037-simple-math-based-question/#findComment-987789 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.