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?
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?
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?
I'd enter 2 + 2 in one textbox, How would I do that using javascript?
And all I want it to do is calculate the equation.
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.
Alright, is there a way to do this in php?
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.
Thank you very much.
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.
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 :)
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 ) ) ) );
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...
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.