On 10/12/06, <b class="gmail_sendername">LK</b> <<a href="mailto:email@example.com">firstname.lastname@example.org</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<?php<br> $x = 3;<br> $y = 4;<br> $calc_str = '$x * $y';<br> eval("echo \"$calc_str\";");<br>?><br><br>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
</blockquote></div><br>I haven't had my coffee yet, but I would guess that it has something to do with those single quotes.<br><br>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.