Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #3707 > unrolled thread
| Started by | "Graham" <graham.stow@stowassocs.co.uk> |
|---|---|
| First post | 2011-11-07 16:25 +0000 |
| Last post | 2011-11-08 11:19 +0100 |
| Articles | 12 — 7 participants |
Back to article view | Back to comp.lang.php
PHP Newbie can't evaluate numerical string as number "Graham" <graham.stow@stowassocs.co.uk> - 2011-11-07 16:25 +0000
Re: PHP Newbie can't evaluate numerical string as number The Natural Philosopher <tnp@invalid.invalid> - 2011-11-07 16:56 +0000
Re: PHP Newbie can't evaluate numerical string as number Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-07 17:52 +0000
Re: PHP Newbie can't evaluate numerical string as number "Graham" <graham.stow@stowassocs.co.uk> - 2011-11-08 08:41 +0000
Re: PHP Newbie can't evaluate numerical string as number Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> - 2011-11-08 09:56 +0100
Re: PHP Newbie can't evaluate numerical string as number "Graham" <graham.stow@stowassocs.co.uk> - 2011-11-08 09:35 +0000
Re: PHP Newbie can't evaluate numerical string as number Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-08 09:57 +0000
Re: PHP Newbie can't evaluate numerical string as number Tim Streater <timstreater@greenbee.net> - 2011-11-08 10:08 +0000
Re: PHP Newbie can't evaluate numerical string as number Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-08 17:49 +0000
Re: PHP Newbie can't evaluate numerical string as number "Graham" <graham.stow@stowassocs.co.uk> - 2011-11-08 10:27 +0000
Re: PHP Newbie can't evaluate numerical string as number Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-07 23:49 +0100
Re: PHP Newbie can't evaluate numerical string as number Jonathan Stein <jstein@image.dk> - 2011-11-08 11:19 +0100
| From | "Graham" <graham.stow@stowassocs.co.uk> |
|---|---|
| Date | 2011-11-07 16:25 +0000 |
| Subject | PHP Newbie can't evaluate numerical string as number |
| Message-ID | <A8OdnXgegKYUmyXTnZ2dnUVZ7ridnZ2d@bt.com> |
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!
[toc] | [next] | [standalone]
| From | The Natural Philosopher <tnp@invalid.invalid> |
|---|---|
| Date | 2011-11-07 16:56 +0000 |
| Message-ID | <j992k0$ko7$1@news.albasani.net> |
| In reply to | #3707 |
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!
>
>
>
why not parse $formula using sscanf?
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-11-07 17:52 +0000 |
| Message-ID | <4eb81acf$0$28518$a8266bb1@newsreader.readnews.com> |
| In reply to | #3707 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Graham" <graham.stow@stowassocs.co.uk> |
|---|---|
| Date | 2011-11-08 08:41 +0000 |
| Message-ID | <f_qdnUg0B6m_diXTnZ2dnUVZ8qudnZ2d@bt.com> |
| In reply to | #3709 |
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'?
"Denis McMahon" <denismfmcmahon@gmail.com> wrote in message
news:4eb81acf$0$28518$a8266bb1@newsreader.readnews.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
[toc] | [prev] | [next] | [standalone]
| From | Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> |
|---|---|
| Date | 2011-11-08 09:56 +0100 |
| Message-ID | <4eb8eed0$0$6947$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #3711 |
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@gmail.com> wrote in message
> news:4eb81acf$0$28518$a8266bb1@newsreader.readnews.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
[toc] | [prev] | [next] | [standalone]
| From | "Graham" <graham.stow@stowassocs.co.uk> |
|---|---|
| Date | 2011-11-08 09:35 +0000 |
| Message-ID | <O5SdndpZ6bFXaiXTnZ2dnUVZ7ridnZ2d@bt.com> |
| In reply to | #3712 |
Yes it works! (although I admit I'm not entirely sure why). Many thanks! And
apologies to Denis for having doubted him.
Thanks also for the warning on using formula in this way. The formula in
question (a bit more complex than A-B) is actually embodied in UK Govenment
legislation, so I won't be letting users mess with it.
R's
Graham Stow
"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> wrote in
message news:4eb8eed0$0$6947$e4fe514c@news2.news.xs4all.nl...
> 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@gmail.com> wrote in message
>> news:4eb81acf$0$28518$a8266bb1@newsreader.readnews.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
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-11-08 09:57 +0000 |
| Message-ID | <4eb8fcf9$0$28445$a8266bb1@newsreader.readnews.com> |
| In reply to | #3713 |
On Tue, 08 Nov 2011 09:35:02 +0000, Graham wrote:
> Yes it works! (although I admit I'm not entirely sure why). Many thanks!
> And apologies to Denis for having doubted him.
Heads up - we don't top post in newsgroups. Reply goes at the bottom so
that reading naturally down the page response is after the comment it
applies to.
If you read the php manual for eval http://uk.php.net/manual/en/
function.eval.php , all is explained:
There are two ways to get a value from eval into the script that calls
eval:
a) use return inside the eval code, and assign the value of eval to a
variable, viz:
$evalResult = eval("return $big_number - $small_number;");
echo $evalResult;
b) assign the result to a variable inside the eval code:
eval("$evalResult = $big_number - $small_number;");
echo $evalResult;
These are the two methods I attempted to show in my example.
Rgds
Denis McMahon
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2011-11-08 10:08 +0000 |
| Message-ID | <timstreater-58A1FE.10084208112011@news.individual.net> |
| In reply to | #3714 |
In article <4eb8fcf9$0$28445$a8266bb1@newsreader.readnews.com>, Denis McMahon <denismfmcmahon@gmail.com> wrote: > Heads up - we don't top post in newsgroups. Reply goes at the bottom so > that reading naturally down the page response is after the comment it > applies to. We? You only speak for yourself. -- Tim "That excessive bail ought not to be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-11-08 17:49 +0000 |
| Message-ID | <4eb96bb6$0$28490$a8266bb1@newsreader.readnews.com> |
| In reply to | #3715 |
On Tue, 08 Nov 2011 10:08:42 +0000, Tim Streater wrote: > In article <4eb8fcf9$0$28445$a8266bb1@newsreader.readnews.com>, > Denis McMahon <denismfmcmahon@gmail.com> wrote: > >> Heads up - we don't top post in newsgroups. Reply goes at the bottom so >> that reading naturally down the page response is after the comment it >> applies to. > > We? You only speak for yourself. I think I can fairly safely claim to speak for the majority of people who actually take part in conversations using usenet (as opposed to people who use websites which present a web based interface to usenet) when I point out that by long standing convention, in usenet, replies go below the comments they refer to. Rgds Denis McMahon
[toc] | [prev] | [next] | [standalone]
| From | "Graham" <graham.stow@stowassocs.co.uk> |
|---|---|
| Date | 2011-11-08 10:27 +0000 |
| Message-ID | <ycydnRhvecuwmSTTnZ2dnUVZ8sednZ2d@bt.com> |
| In reply to | #3714 |
"Denis McMahon" <denismfmcmahon@gmail.com> wrote in message
news:4eb8fcf9$0$28445$a8266bb1@newsreader.readnews.com...
> On Tue, 08 Nov 2011 09:35:02 +0000, Graham wrote:
>
>> Yes it works! (although I admit I'm not entirely sure why). Many thanks!
>> And apologies to Denis for having doubted him.
>
> Heads up - we don't top post in newsgroups. Reply goes at the bottom so
> that reading naturally down the page response is after the comment it
> applies to.
>
> If you read the php manual for eval http://uk.php.net/manual/en/
> function.eval.php , all is explained:
>
> There are two ways to get a value from eval into the script that calls
> eval:
>
> a) use return inside the eval code, and assign the value of eval to a
> variable, viz:
>
> $evalResult = eval("return $big_number - $small_number;");
> echo $evalResult;
>
> b) assign the result to a variable inside the eval code:
>
> eval("$evalResult = $big_number - $small_number;");
> echo $evalResult;
>
> These are the two methods I attempted to show in my example.
>
> Rgds
>
> Denis McMahon
I hang my head in shame! Thinking about it, i've been critised for top
posting before, but couldn't remember the drill as I post to newsgroups so
rarely. Thanks for being patient and polite (unlike some). And thanks for
the very thorough explanation which I now follow.
R's
Graham Stow
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-11-07 23:49 +0100 |
| Message-ID | <5823112.UsPfZmn7uG@PointedEars.de> |
| In reply to | #3707 |
Graham wrote:
> I'm a Perl veteran (well almost!) but a PHP newbie. […]
> <?php
> $formula = "!big_number - !small_number";
> ?>
> […]
> $big_number = 30000;
> $small_number = 3000;
> $formula = str_replace("!", "$", $formula);
> eval("\$formula = \"$formula\";");
You don't want a string, so don't make a string – remove the `\"'.
(I don't think Perl is so much different in that regard.)
> […]
> The above gives me...'formula = 30000 - 3000'
> when of course what I want is...'formula = 27000'
HTH
PointedEars
--
When all you know is jQuery, every problem looks $olvable.
[toc] | [prev] | [next] | [standalone]
| From | Jonathan Stein <jstein@image.dk> |
|---|---|
| Date | 2011-11-08 11:19 +0100 |
| Message-ID | <4eb9021c$0$294$14726298@news.sunsite.dk> |
| In reply to | #3707 |
Den 07-11-2011 17:25, Graham skrev:
> <?php
> $formula = "!big_number - !small_number";
> ?>
function myFormula($big_number, $small_number) {
return $big_number - $small_number;
}
> <?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");
> ?>
include 'vars.php';
print myFormula(30000, 3000);
Regards
Jonathan
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web