Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
The solution is already in pear : http://pear.php.net/package/Math_RPN
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
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; } On Fri, December 7, 2007 8:51 am, Nathan Rixham wrote: > 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- 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? -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
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: On 12/7/07, Mike <[EMAIL PROTECTED]> wrote: 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
Antony Dovgal wrote: On 07.12.2007 18:05, Alexey Zakhlestin wrote: 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. 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
On 12/7/07, Mike <[EMAIL PROTECTED]> wrote: > 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/
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
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 On Fri, 2007-12-07 at 14:51 +, Nathan Rixham wrote: > 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 <[EMAIL PROTECTED]> signature.asc Description: This is a digitally signed message part
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
Daniel and Derick, thanks for the replies.. Daniel first.. 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 <[EMAIL PROTECTED]> 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 $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" 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
On Fri, 2007-12-07 at 16:44 +0100, Derick Rethans wrote: > On Fri, 7 Dec 2007, Nathan Rixham wrote: > > > 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? > > 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! ... -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
On Fri, 7 Dec 2007, Nathan Rixham wrote: > 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? http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
On Dec 7, 2007 9:51 AM, Nathan Rixham <[EMAIL PROTECTED]> 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 > $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" 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 > > -- 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. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
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: On 07.12.2007 18:05, Alexey Zakhlestin wrote: 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. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
On 07.12.2007 18:05, Alexey Zakhlestin wrote: > 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Thoughts on Feature Request - Arithmetic
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) On 12/7/07, Nathan Rixham <[EMAIL PROTECTED]> wrote: > 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Alexey Zakhlestin http://blog.milkfarmsoft.com/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php