Jump to content

Simple math based question


Irresistable

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.