Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/187037-simple-math-based-question/
Share on other sites

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);
?>

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?

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.