variables in math question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pineriver
Forum Commoner
Posts: 50
Joined: Fri Aug 15, 2003 5:24 pm

variables in math question

Post by Pineriver »

Hello,
I am wondering if it is all possible to have a variable operate a math command like...

$set = '+';//$set comes from a database and it changes

echo (10 $set 4); // would return 14

This doesnt seem to work so I am wondering if I would have to do

if($set == '+'){ echo (10 + 4); }

if anyone can figure out how to do that other way please share, thanks.
It would be a big time saver since there are 6 of these
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Code: Select all

$set = '+';//$set comes from a database and it changes 
echo eval("return 10 $set 4;");
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

there are only 2 things you need to know:

validation, so people don't request evil things from your string
evaluation of the expression

a very dirty sample can be found at
http://home.mysth.be/~timvw/programming ... ulator.txt
Post Reply