# Thoughts on Feature Request - Arithmetic

- Thread: https://externals.io/message/33828
- Original archive: https://news-web.php.net/php.internals/33828
- Started by Nathan Rixham on 2007-12-07T14:51:01+00:00
- Messages: 14
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #33828 — Nathan Rixham — 2007-12-07T14:51:01+00:00

URL: https://externals.io/message/33828#33828

In-Built PHP Functions for parsing of basic arithmetic and if possible 
fraction to decimal and decimal to fraction

$arithmetic_string = "3*5";
echo arith($arithmetic_string); // returns float 15

$arithmetic_string = "1/2";
echo arith($arithmetic_string); // returns float 0.5

$fraction_string = "1/4";
echo fracdec($fraction_string); // returns float 0.25

$dec_string = "0.5";
echo decfrac($dec_string); // returns string "1/4"

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

---

## #33829 — Alexey Zakhlestin — 2007-12-07T15:05:57+00:00

URL: https://externals.io/message/33828#33829 · In reply to: #33828

I doubt this is needed in core, but sounds "ok" for an extension.
Also, I believe, quite a decent version of this can be written in php
(think PEAR)


> In-Built PHP Functions for parsing of basic arithmetic and if possible
> fraction to decimal and decimal to fraction
>
> $arithmetic_string = "3*5";
> echo arith($arithmetic_string); // returns float 15
>
> $arithmetic_string = "1/2";
> echo arith($arithmetic_string); // returns float 0.5
>
> $fraction_string = "1/4";
> echo fracdec($fraction_string); // returns float 0.25
>
> $dec_string = "0.5";
> echo decfrac($dec_string); // returns string "1/4"
>
> 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
>
> --
> 
> 
>
>


-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #33830 — Antony Dovgal — 2007-12-07T15:24:55+00:00

URL: https://externals.io/message/33828#33830 · In reply to: #33829

> I doubt this is needed in core, but sounds "ok" for an extension.

Yes, I'm sure a simple extension using re2c to parse the expressions 
would be gladly accepted into PECL.
I don't see any need for this in the core, though.

> Also, I believe, quite a decent version of this can be written in php
> (think PEAR)

This could be an option either.

-- 
Wbr, 
Antony Dovgal

---

## #33831 — Nathan Rixham — 2007-12-07T15:34:27+00:00

URL: https://externals.io/message/33828#33831 · In reply to: #33830

Agreed, PECL or PEAR, some provision should be made, it's worth the 
extra few bytes of code.

Thanks for the opinions

Nathan

Antony Dovgal wrote:
> 
> > […]
> 
> Yes, I'm sure a simple extension using re2c to parse the expressions 
> would be gladly accepted into PECL.
> I don't see any need for this in the core, though.
> 
> > […]
> 
> This could be an option either.

---

## #33833 — Daniel Brown — 2007-12-07T15:37:49+00:00

URL: https://externals.io/message/33828#33833 · In reply to: #33828

> 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
>
> --
> 
> 
>
>



-- 
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.

---

## #33834 — Derick Rethans — 2007-12-07T15:44:53+00:00

URL: https://externals.io/message/33828#33834 · In reply to: #33828

> In-Built PHP Functions for parsing of basic arithmetic and if possible
> fraction to decimal and decimal to fraction
> 
> $arithmetic_string = "3*5";
> echo arith($arithmetic_string); // returns float 15

What's wrong with eval?

<?php
eval( '$res = 3 * 5;' );
echo $res, "\n";

regards,
Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org

---

## #33837 — Nathan Rixham — 2007-12-07T16:19:18+00:00

URL: https://externals.io/message/33828#33837 · In reply to: #33833

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:
> 
> > […]
> 
>     PHP already handles half of what you're looking for by default.
> 
> > […]
> 
>     <?=3 * 5;?>
> 
> > […]
> 
>     <?=1 / 2;?>
> 
> > […]
> 
>     <?=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.
> 
> > […]

---

## #33838 — Robert Cummings — 2007-12-07T16:23:45+00:00

URL: https://externals.io/message/33828#33838 · In reply to: #33834

> 
> 
> > […]
> 
> What's wrong with eval?
> 
> <?php
> eval( '$res = 3 * 5;' );
> echo $res, "\n";

Maybe it's from user input? And it is generally taught that if eval() is
the solution then you're on the wrong track.

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

---

## #33840 — Mike — 2007-12-07T17:37:13+00:00

URL: https://externals.io/message/33828#33840 · In reply to: #33828

Yes, an easy way to handle this functionality that is safe to use with
user input would be REALLY nice. 

Specifically for allowing users to specify custom formulas to do all
sorts of nifty stuff. Especially in reporting and payroll/commission
applications. 

We're currently working on a reporting class where we would like the
user to able to specify custom columns in XML, ie:

$col5 = ($col1 / $col2) * ($col3 / $col4)

Or in our payroll application where customers want to calculate custom
commissions for sales people:

$commission = ($gross_sales - $gross_pay - $medical_benefits -
$chargebacks) * $employee_commission_bracket





> In-Built PHP Functions for parsing of basic arithmetic and if possible 
> fraction to decimal and decimal to fraction
> 
> $arithmetic_string = "3*5";
> echo arith($arithmetic_string); // returns float 15
> 
> $arithmetic_string = "1/2";
> echo arith($arithmetic_string); // returns float 0.5
> 
> $fraction_string = "1/4";
> echo fracdec($fraction_string); // returns float 0.25
> 
> $dec_string = "0.5";
> echo decfrac($dec_string); // returns string "1/4"
> 
> 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
> 
-- 
Mike <ipso@snappymail.ca

---

## #33841 — Alexey Zakhlestin — 2007-12-07T17:43:39+00:00

URL: https://externals.io/message/33828#33841 · In reply to: #33840

> Yes, an easy way to handle this functionality that is safe to use with
> user input would be REALLY nice.
>
> Specifically for allowing users to specify custom formulas to do all
> sorts of nifty stuff. Especially in reporting and payroll/commission
> applications.
>
> We're currently working on a reporting class where we would like the
> user to able to specify custom columns in XML, ie:
>
> $col5 = ($col1 / $col2) * ($col3 / $col4)
>
> Or in our payroll application where customers want to calculate custom
> commissions for sales people:
>
> $commission = ($gross_sales - $gross_pay - $medical_benefits -
> $chargebacks) * $employee_commission_bracket

In this case, you might find embedding usable…

http://pecl.php.net/package/lua
http://pecl.php.net/package/python
http://pecl.php.net/package/perl

-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #33843 — Hartmut Holzgraefe — 2007-12-07T18:21:26+00:00

URL: https://externals.io/message/33828#33843 · In reply to: #33830

Antony Dovgal wrote:
> 
> > […]
> 
> Yes, I'm sure a simple extension using re2c to parse the expressions 
> would be gladly accepted into PECL.

or something libeval based ...

http://members.bellatlantic.net/~dutky/libeval.html

i already started a pecl wrapper for that onece
but somehow lost interest. Don't remember why,
it was either license issues (it's LGPL now but
might have been GPL back then) or build system
issues on the library itself ...

i'll give it another look tonight in parallel
to the TV maybe ...

-- 
Hartmut Holzgraefe, Principal Support Engineer
               .
Discover new MySQL Monitoring & Advisory features at:
http://www.mysql.com/products/enterprise/whats_new.html

Hauptsitz: MySQL GmbH, Dachauer Str.37, 80335 München
Geschäftsführer: Kaj Arnö - HRB München 162140

---

## #33844 — Nathan Rixham — 2007-12-07T18:41:44+00:00

URL: https://externals.io/message/33828#33844 · In reply to: #33841

I've thought about this for the remainder of the day, and I can't see 
any good reason why this functionality should not be implemented into 
the core; it's basic fucntionality, a safe way of doing things and 
couldn't take more than a day or two to add in from a single developer, 
and surely it'd be no more than a couple of kb's worth compiled, if that!

Also, it's a bit of minor piece of functionality to warrent a PECL/PEAR 
Extension, and even more hassle for somebody to reasearch, find and 
download it - all to do some basic arithmetic.

Can anybody give me an argument as to why this functionality should not 
be incorporated into the core?

Many Regards

Nathan Rixham


Alexey Zakhlestin wrote:
> 
> > […]
> 
> In this case, you might find embedding usable…
> 
> http://pecl.php.net/package/lua
> http://pecl.php.net/package/python
> http://pecl.php.net/package/perl

---

## #33861 — Richard Lynch — 2007-12-08T01:50:28+00:00

URL: https://externals.io/message/33828#33861 · In reply to: #33828

I suspect you could address security concerns by limiting the input to
valid characters in your arithmetic needs:

if (preg_match('|^[0-9.+/*-]+$|', $expression)){
  eval("$foo = $expression");
  return $foo;
}


> In-Built PHP Functions for parsing of basic arithmetic and if possible
> fraction to decimal and decimal to fraction
>
> $arithmetic_string = "3*5";
> echo arith($arithmetic_string); // returns float 15
>
> $arithmetic_string = "1/2";
> echo arith($arithmetic_string); // returns float 0.5
>
> $fraction_string = "1/4";
> echo fracdec($fraction_string); // returns float 0.25
>
> $dec_string = "0.5";
> echo decfrac($dec_string); // returns string "1/4"
>
> 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
>
> --
> 
> 
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

---

## #33877 — Lokrain — 2007-12-09T00:48:15+00:00

URL: https://externals.io/message/33828#33877

The solution is already in pear : http://pear.php.net/package/Math_RPN
