[nycphp-talk] Eval question

LK lk613m at
Thu Oct 12 11:30:28 EDT 2006

The reason I don't want to use regexp is because eventually I want to have an arbitrary formula for $calc_str, and I don't want to design my own evaluation compiler with regexp - that's what eval() is supposed to do, or so it seems. 

----- Original Message ----
From: Chris Merlo <chris at>
To: NYPHP Talk <talk at>
Sent: Thursday, October 12, 2006 10:26:43 AM
Subject: Re: [nycphp-talk] Eval question

On 10/12/06, LK <lk613m at> wrote:
  $x = 3;
  $y = 4;
  $calc_str = '$x * $y';
  eval("echo \"$calc_str\";");

I want to evaluate the expression $x * $y (x times y). But when I run it thru the eval() function it returns "3 * 4" instead of "12". It seems to interpret * as a string instead of a multiplication operator. Any suggestions greatly appreciated 

I haven't had my coffee yet, but I would guess that it has something to do with those single quotes.

But there's a bigger question here:  Why do you want to store the expression in a string?  Just store the $x and the $y, and multiply them when you have to.  If there's some larger issue at hand, like you have to read the expressions from a file or something, use the regexp functions to get the two operands and the operator, and then do the math you have to do. 

New York PHP Community Talk Mailing List

NYPHPCon 2006 Presentations Online

Show Your Participation in New York PHP

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the talk mailing list