NYCPHP Meetup

NYPHP.org

[nycphp-talk] Eval question

LK lk613m at yahoo.com
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 theyellowbox.com>
To: NYPHP Talk <talk at lists.nyphp.org>
Sent: Thursday, October 12, 2006 10:26:43 AM
Subject: Re: [nycphp-talk] Eval question

On 10/12/06, LK <lk613m at yahoo.com> wrote:
<?php
  $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. 
-c

_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20061012/51eaabd2/attachment.html>


More information about the talk mailing list