Jump to content

Archived

This topic is now archived and is closed to further replies.

Intelly XAD

Calculate a string

Recommended Posts

Hi there, i've created a function that generates variate sums, for example: 5 * 4 / 20 - 5 =
This sum is saved under a string $sum="5 * 4 / 20 - 5 ="
But now I want the PHP code to calculate this sum for met is there any function available in PHP that can do this?
for example: calc($sum);

Many thnx in advance for the answers

Share this post


Link to post
Share on other sites
try
[code]<?php
eval('$sum = 5 * 4 / 20 - 5 ;');
echo $sum;
?>[/code]

--> -4

Share this post


Link to post
Share on other sites
Youre method works in the case you type it but when I type somthing like this it doesn't work:
<?php
$sum="5 + 20 / 5 - 4";
eval('$sum = $sum;');
?>

Share this post


Link to post
Share on other sites
You are using single quotes. Variables dont work within single quotes. Use double quotes.

Share this post


Link to post
Share on other sites
try[code]<?php
$sum="5 + 20 / 5 - 4";
eval("\$sum = $sum;");
echo $sum;
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.