Intelly XAD Posted September 3, 2006 Share Posted September 3, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19602-calculate-a-string/ Share on other sites More sharing options...
Barand Posted September 3, 2006 Share Posted September 3, 2006 try[code]<?phpeval('$sum = 5 * 4 / 20 - 5 ;');echo $sum;?>[/code]--> -4 Link to comment https://forums.phpfreaks.com/topic/19602-calculate-a-string/#findComment-85310 Share on other sites More sharing options...
Intelly XAD Posted September 4, 2006 Author Share Posted September 4, 2006 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;');?> Link to comment https://forums.phpfreaks.com/topic/19602-calculate-a-string/#findComment-85907 Share on other sites More sharing options...
wildteen88 Posted September 4, 2006 Share Posted September 4, 2006 You are using single quotes. Variables dont work within single quotes. Use double quotes. Link to comment https://forums.phpfreaks.com/topic/19602-calculate-a-string/#findComment-85909 Share on other sites More sharing options...
sasa Posted September 4, 2006 Share Posted September 4, 2006 try[code]<?php$sum="5 + 20 / 5 - 4";eval("\$sum = $sum;");echo $sum;?>[/code] Link to comment https://forums.phpfreaks.com/topic/19602-calculate-a-string/#findComment-85916 Share on other sites More sharing options...
Intelly XAD Posted September 5, 2006 Author Share Posted September 5, 2006 Many thnx that worked... Link to comment https://forums.phpfreaks.com/topic/19602-calculate-a-string/#findComment-86459 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.