Re: math in PHP

From: Date: Sat, 18 Sep 2004 21:34:47 +0000
Subject: Re: math in PHP
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-197128@lists.php.net to get a copy of this message
$var = "1+2+3+4+5"; // should be 15 eval('$result = ('.$var.');'); echo $result; // returns 15 --- could also be written as: eval("\$result = ($var);"); but that's not the point ;) Whitehawk Stormchaser wrote:
Hi! I have been trying to make some sort of calculator in PHP, but I have stumbled on a problem... I have been make some kind of dynamical calculator, unlike most I have seen, that had $value1 [math operator] $value2 strict, but a calculator, that can calculate basically anything ( even something like (5*(8-3)+2*(4-1))^3 ), and can be put in as string variable... I have tried things like this: $var = "1+1+1+1"; echo $var + 0; //returns 1 or $var = "1+1+1+1"; echo eval("\$var;"); //returns blank or $var = "1+1+1+1"; echo (int) $var; //returns 1 things like that... Now my question is: is it possible to calculate the value of the string somehow? Thanks. Stormy.


« previous php.general (#197128) next »