miffe Posted July 30, 2007 Share Posted July 30, 2007 Say, I have a string with a math function, just divisions, multiplications, sums and substractions... and I have an integer..$b = 5;$a = "/2+4";Is there any way to make $c = $a (operations in $ ?I guess I could write a function to trim the string and detect every single character and then do the maths and return the values, but it would take me ages :\ does anyone have any preset function or an "include" ? Thank you :)Miffe Link to comment Share on other sites More sharing options...
jhecht Posted July 30, 2007 Share Posted July 30, 2007 Why not just use PHP to do the math, WITHOUT the strings? Link to comment Share on other sites More sharing options...
aspnetguy Posted July 30, 2007 Share Posted July 30, 2007 use eval //example$a = "1+1";$b = eval($a); //$b = 2 Link to comment Share on other sites More sharing options...
miffe Posted July 30, 2007 Author Share Posted July 30, 2007 use eval//example$a = "1+1";$b = eval($a); //$b = 2 thanks Link to comment Share on other sites More sharing options...
miffe Posted July 30, 2007 Author Share Posted July 30, 2007 use eval$subtotal = Round($modls[$y][17] + ($modls[$y][18]+($modls[$y][19]*$modls[$y][4])));$semitotal = $subtotal . $operaciones;echo "<tr><td>" . $semitotal . "</td></tr>";$total = eval($semitotal);$modls[$y][20] = $total; The result from that echo (debug) is:0/11/12/14/129/1149/2(It is part of a loop of numbers... it should, theorically, work shouldn't it?) Link to comment Share on other sites More sharing options...
justsomeguy Posted July 30, 2007 Share Posted July 30, 2007 Whatever you send to eval needs to be a legal PHP statement. That means it needs to end with a semicolon and follow all the other rules that you would normally use. Link to comment Share on other sites More sharing options...
miffe Posted July 30, 2007 Author Share Posted July 30, 2007 Whatever you send to eval needs to be a legal PHP statement. That means it needs to end with a semicolon and follow all the other rules that you would normally use.Yeah I checked out http://www.php.net/evalbut even so I don't understand what I have to do in my case... it's too complex for my understanding the use of the \'s and things. I just wanted to evaluate a string math operation and get a variable result :\ Link to comment Share on other sites More sharing options...
miffe Posted July 30, 2007 Author Share Posted July 30, 2007 Yeah I checked out http://www.php.net/evalbut even so I don't understand what I have to do in my case... it's too complex for my understanding the use of the \'s and things. I just wanted to evaluate a string math operation and get a variable result :\Here's my code, yes it is giving me the total, lets say for example the first product has a $subtotal of $147 when $operaciones is /1but it keeps having $147 even though $operaciones is (147)*0.85+1.2 $subtotal = Round($modls[$y][17] + ($modls[$y][18]+($modls[$y][19]*$modls[$y][4]))); $semitotal = strval($subtotal) . $operaciones; eval("\$total = \"$semitotal\";"); $modls[$y][20] = $total; Link to comment Share on other sites More sharing options...
justsomeguy Posted July 30, 2007 Share Posted July 30, 2007 You're putting the semitotal in quotes in the string. That is still making a string, not executing the code.Echo the line of code that you are sending to eval. If you can run that line of code on its own, then it will work. If you get a syntax error when you try to run the code, then you'll get the same error if you use eval to run it. Link to comment Share on other sites More sharing options...
miffe Posted July 30, 2007 Author Share Posted July 30, 2007 Here's my code, yes it is giving me the total, lets say for example the first product has a $subtotal of $147 when $operaciones is /1but it keeps having $147 even though $operaciones is (147)*0.85+1.2 $subtotal = Round($modls[$y][17] + ($modls[$y][18]+($modls[$y][19]*$modls[$y][4]))); $semitotal = strval($subtotal) . $operaciones; eval("\$total = \"$semitotal\";"); $modls[$y][20] = $total; perfect, thanks a lot...that seems to have worked * solved * Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.