Jump to content


Photo

Calculate a string


  • Please log in to reply
5 replies to this topic

#1 Intelly XAD

Intelly XAD
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationNetherlands

Posted 03 September 2006 - 07:07 PM

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
Experience does the trick...

An amateur is commonly better than someone who studied for it...

Don't mind my English, trust me, don't...

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 September 2006 - 07:46 PM

try
<?php
eval('$sum = 5 * 4 / 20 - 5 ;');
echo $sum;
?>

--> -4
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Intelly XAD

Intelly XAD
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationNetherlands

Posted 04 September 2006 - 06:22 PM

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;');
?>

Experience does the trick...

An amateur is commonly better than someone who studied for it...

Don't mind my English, trust me, don't...

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 06:24 PM

You are using single quotes. Variables dont work within single quotes. Use double quotes.

#5 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 04 September 2006 - 06:42 PM

try
<?php
$sum="5 + 20 / 5 - 4";
eval("\$sum = $sum;");
echo $sum;
?>


#6 Intelly XAD

Intelly XAD
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationNetherlands

Posted 05 September 2006 - 03:28 PM

Many thnx that worked...
Experience does the trick...

An amateur is commonly better than someone who studied for it...

Don't mind my English, trust me, don't...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users