0

Hi,

I'm new to Daniweb, and I was just wondering, let's say I entered: 2 + 2 in a textbox, and using php and a function I want to solve that equation. But whenever I try to return it It returns "2 + 2". Can anyone help me with this?

5
Contributors
12
Replies
13
Views
6 Years
Discussion Span
Last Post by mschroeder
0

Hi timpogue,

I'd recommend you use JavaScript to return the value dynamically. PHP, by itself, is not capable of doing so (that I know of). It would have to be used in conjointly with AJAX or something of that sort.

Also, do you mean you enter "2 + 2" or do you enter "2", click the add button, and enter "2" again?

0

I'd enter 2 + 2 in one textbox, How would I do that using javascript?

0

OK.

Split the string using the split() method. [Source]

Create a variable for the numbers and make sure to use parseInt(string) to convert the variables into actual numbers.

0

Again, I'm not 100% sure as my PHP knowledge is still a work in progress. The thing is, more than likely you'll need to "refresh" the page or go to another to get the result. Otherwise, you'll need to implement AJAX with PHP to get the result on that page without any refresh or redirect. That is what I think. Make sure to ask others about this.

Here's a quick example I've concocted. JavaScript solution...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<script type="text/javascript">
function calculate()
{
	var string = document.getElementById('calculate').value;
	var numbers = new Array();
	numbers = string.split(' + ');
	var sum = 0;
	
	for(var i = 0; i < numbers.length; i++)
	{
		alert("number " + (i+1) + " is " + numbers[i]);
		sum = sum + parseInt(numbers[i]);
	}
	
	alert("sum is " + sum);
}
</script>
<input type="text" id="calculate" />
<input type="button" onclick="calculate()" value="calc" />
</body>

</html>

---EDIT---

It is by no means the best way and it's not proper coding, but I wanted to show you a simple example.

Edited by floatingDivs: n/a

0

If this was always going to be two numbers and a mathematical operation, then the easiest way in PHP would be to have a box for the first number, a drop down list for the operation and a box for the second number. It is then easy enough in the module that processes the form to use an if / else or a switch/ case construct to determine what the operation is and perform that operation on the two numbers. If you want it to be free-form then you would need to parse the string to get back to the same thing. If you don't want to limit it to 2 numbers and one operation then you are getting into the low end of compiler / interpreter writing and that would be more complex.

0

you can also do this simple on php.

this is very simple function. ha ha

<?php
funtion foo(){
    // first receive the post variable from the form

$bar=$_POST['num'] ;

$numberDetails= split ('+',$bar); // split and take details
for ($c=0;$c<count($numberDetails);$c++){
// basic run through

$hoo+=$numberDetails[$c];



}
echo "The sum is ".$hoo; // echo answer. 

}
?>

ha ha have fun
all in less than 2 minutes
Explore :)

Edited by richieking: n/a

0

you can also do this simple on php.

this is very simple function. ha ha

<?php
funtion foo(){
    // first receive the post variable from the form

$bar=$_POST['num'] ;

$numberDetails= split ('+',$bar); // split and take details
for ($c=0;$c<count($numberDetails);$c++){
// basic run through

$hoo+=$numberDetails[$c];



}
echo "The sum is ".$hoo; // echo answer. 

}
?>

ha ha have fun
all in less than 2 minutes
Explore :)

Again, split is depreciated as of 5.3. If you're developing on ANY version of php at this point you should avoid it, in favor of other options which already exist.

Just an FYI its bad form to have a function call in the second portion of a for loop. for ($c=0;$c<count($numberDetails);$c++) will call count( array ) on every iteration of your loop. 3 array keys no big deal. Thousands it will add up. for ($c=0,$count = count($numberDetails); $c < $count; $c++){ http://php.net/manual/en/control-structures.for.php

Another solution to the problem.

<?php
$string = '2 + 2';
echo array_sum( explode('+', str_replace( ' ', '', trim( $string ) ) ) );
0

wow, i never thought of doing that, but wouldn't it be easier to do this?

<?php
$equationraw = $_POST['equation'];
eval("\$equation = \"$equationraw\";");
echo $equation;

Because that would basically support multiplication, division, addition, subtraction, etc...

0

eval should be avoided at all costs. Especially if you're passing user supplied information into it.

I saw a really good implementation talking about creating a mathematical parser that could take an equation as a string and parse it out to its components and then evaluate it using order of operations, but for the life of me, I can't find at the moment.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.