2

I have a string '1/2' and i need to get a float value 0.5 from it.

Other examples: Operation: $operation = '5/8'; Wanted value: $value = 0.625;

  • 4
    `list($x, $y) = explode('/', $operation); $value = $x / $y;` – Mark Baker Mar 05 '15 at 17:44
  • 1
    Probably you're looking for the `eval` function. http://php.net/manual/en/function.eval.php – Andy Lester Mar 05 '15 at 17:47
  • 3
    possible duplicate of [calculate math expression from a string using eval](http://stackoverflow.com/questions/18880772/calculate-math-expression-from-a-string-using-eval) – Rizier123 Mar 05 '15 at 17:47
  • 1
    @AndyLester Bad idea to use: `evil()` – Rizier123 Mar 05 '15 at 17:48
  • 1
    Blanket statements like "bad idea to use" aren't helpful in evaluating when one should use them. If you want to help OP, explain the potential problems. – Andy Lester Mar 05 '15 at 17:48
  • Or you can try this : http://stackoverflow.com/questions/1015242/how-to-evaluate-formula-passed-as-string-in-php?answertab=votes#tab-top – Niols Mar 05 '15 at 17:49
  • 1
    If this value is coming from user input, then eval() is potentially a very bad idea.... not only are there security risks with feeding any user input to eval (what if the user inputs `unlink(__FILE__)` or similar), but adequate error handling for malformed input to eval() is also non-existent, making it almost impossible to diagnose problems – Mark Baker Mar 05 '15 at 17:53
  • 1
    If you want a clean, secure calculator that can handle a range of mathematical expressions (not simply division) then take a look at [how to make a calculator in php](http://stackoverflow.com/questions/12692727/how-to-make-a-calculator-in-php) – Mark Baker Mar 05 '15 at 17:55

3 Answers3

4

You can use eval, also please note your are not looking for an int as they are whole numbers.

<?php

$operation = '1/2';

eval('$value = ' . $operation . ';');

echo $value;

?>
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
1

This will work if you only have one "/".

$str = '2/5';
$newStr = explode('/', $str);
echo $newStr[0]/$newStr[1];
SebHallin
  • 881
  • 6
  • 11
-1

Edited. Not elegant because eval is used, but working.

$operation = '1/2';

function stringCalc($operation){

    return eval('return '.$operation.';');
}

$value = stringCalc($operation);
Mateusz Mania
  • 839
  • 4
  • 15
  • Sorry. Answer edited, my second solution. Okay, now i see other user gives You same solution ... – Mateusz Mania Mar 05 '15 at 18:02
  • Btw. I found few questions, like this. http://stackoverflow.com/questions/14999578/php-calculate-formula-in-string http://stackoverflow.com/questions/5057320/php-function-to-evaluate-string-like-2-1-as-arithmetic-2-1-1 http://stackoverflow.com/questions/18880772/calculate-math-expression-from-a-string-using-eval http://stackoverflow.com/questions/25469528/convert-string-into-math-equation http://stackoverflow.com/questions/28586982/risks-of-using-php-eval-for-string-math http://stackoverflow.com/questions/1015242/how-to-evaluate-formula-passed-as-string-in-php Maybe You found better answer. – Mateusz Mania Mar 06 '15 at 01:04