Re: Thoughts on Feature Request - Arithmetic

From: Date: Fri, 07 Dec 2007 16:19:18 +0000
Subject: Re: Thoughts on Feature Request - Arithmetic
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-33837@lists.php.net to get a copy of this message
Daniel and Derick, thanks for the replies.. Daniel first.. <?=3 * 5;?> is great :) however $arithmetic_string = "3*5"; $arithmetic_value = arith($arithmetic_string); not possible with the method you mentioned. eval() yes this works, however personally I feel a simple arith() (or more suitably named function) would be a great deal quicker and safer than eval($_POST['sum']), the security dangers are somewhat obvious. ps: I must point out that I've still never had a use for this and never would (perhaps the fraction??), but somebody over on a dev forum does, and I was quite suprised this functionality wasn't included! :P Nathan Daniel Brown wrote:
On Dec 7, 2007 9:51 AM, Nathan Rixham <nrixham@gmail.com> wrote:
In-Built PHP Functions for parsing of basic arithmetic and if possible fraction to decimal and decimal to fraction
    PHP already handles half of what you're looking for by default.
$arithmetic_string = "3*5"; echo arith($arithmetic_string); // returns float 15
    <?=3 * 5;?>
$arithmetic_string = "1/2"; echo arith($arithmetic_string); // returns float 0.5
    <?=1 / 2;?>
$fraction_string = "1/4"; echo fracdec($fraction_string); // returns float 0.25
    <?=1 / 4; ?>
$dec_string = "0.5"; echo decfrac($dec_string); // returns string "1/4"
    Now I see why you need PHP to do your math for you!  ;-P
    There are functions written that do this now though.
I've used php for years, came accross the need for this today and was suprised such a basic command wasn't there.. currently the only way I can see to do it would be to eval.. which isn't the most secure method for such a basic need. Regards Nathan -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.internals (#33837) next »