[nycphp-talk] Eval question
chris at theyellowbox.com
Thu Oct 12 10:26:43 EDT 2006
On 10/12/06, LK <lk613m at yahoo.com> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the talk