FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » PHP Newbie can't evaluate numerical string as number
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: PHP Newbie can't evaluate numerical string as number [message #175924 is a reply to message #175923] Tue, 08 November 2011 08:56 Go to previous messageGo to previous message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma:
Senior Member
On 11/8/2011 9:41 AM, Graham wrote:
> Unfortunately not. Here you introduce the formula into the main script,
> whereas I need to introduce it in a separate script (or import it from a
> database) to save me having to amend it countless times if it ever changes.
> Hence me using str_replace to change !big_number to $big_number. Although
> this worked, I was still left with a 'numerical string' which I couldn't
> evaluate. So I guess my question is how do you evaluate a numerical string
> such as '30000 - 3000'?

I think Dennis showed that trick: use eval().
Personally I think it is ugly, but it works.

example:
$myNumericString = "30000 - 300";
$myEval = '$myResult = '.$myNumericString.';';
eval ($myEval);
echo $myResult;

(not tested)

A warning:
I once had to develop a webside where people needed to enter formula's
that worked on data (a bit like MS Excel, but serverside).
For example:
"SUM(**somefield**) / COUNT(**somefield**)"
They needed to be stored in a database too, and the number of possible
manipulations (like SUM and COUNT) were limited.
If I were to eval() that string blindly, I would open up a the side to a
lot of missery (naughty commands, eg file_put_contents()), so I ended up
writing a parser that took the formula apart, and if it used anything
outside my small list of allowed manipulations, I refused the formula.

I am not sure where your formula's come from, but if they are entered by
users, take some care before using eval().


Regards,
Erwin Moller


>
> "Denis McMahon"<denismfmcmahon(at)gmail(dot)com> wrote in message
> news:4eb81acf$0$28518$a8266bb1(at)newsreader(dot)readnews(dot)com...
>> On Mon, 07 Nov 2011 16:25:42 +0000, Graham wrote:
>>
>>> I'm a Perl veteran (well almost!) but a PHP newbie. I've got a simple
>>> subtraction formula in 'vars.php' as follows:
>>>
>>> <?php
>>> $formula = "!big_number - !small_number"; ?>
>>>
>>> I then include it in 'test.php' along with a few variables, do a bit of
>>> string replacement, run eval on it, then print it
>>>
>>> <?PHP
>>> include 'vars.php';
>>> $big_number = 30000;
>>> $small_number = 3000;
>>> $formula = str_replace("!", "$", $formula); eval("\$formula =
>>> \"$formula\";");
>>> //$formula = intval($formula);
>>> //settype($formula, "integer");
>>> print ("formula = $formula\n");
>>> ?>
>>>
>>> The above gives me...'formula = 30000 - 3000' when of course what I want
>>> is...'formula = 27000'
>>>
>>> A few of the many things I've tried are commented out. Please put me out
>>> of my misery someone!
>>
>> Does the following working example help?
>>
>> <?php
>>
>> $big_number = 50000;
>> $small_number = 5000;
>>
>> $formula1 = "\$result1 = \$big_number - \$small_number;";
>> $formula2 = "return(\$big_number - \$small_number);";
>>
>> echo "\$formula1 = '{$formula1}'\n";
>> echo "\$formula2 = '{$formula2}'\n";
>>
>> eval($formula1);
>> $result2 = eval($formula2);
>>
>> echo "\$result1 = {$result1}\n";
>> echo "\$result2 = {$result2}\n";
>>
>> ?>
>>
>> Rgds
>>
>> Denis McMahon
>
>


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: shiping cart program
Next Topic: he discount UGG boots are best known for its "Classic" sheepskin boots.
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 07 11:23:27 GMT 2024

Total time taken to generate the page: 0.04818 seconds