[SOLVED]Using the value of variable in the php code...
Moderator: General Moderators
[SOLVED]Using the value of variable in the php code...
I'm trying to build a simple calculator in php and I have realised that I may need lots of switch statements which I cant really be bothered to do (It is Friday afternoon, after all). So i was wondering if it was possible to use the value of a variable, such as a plus sign (+) in an expression?
Last edited by dhrosti on Fri Mar 09, 2007 10:14 am, edited 1 time in total.
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
If you use the variable to construct the operation as a string, eval() could be used to evaluate it.
Im getting on better then i was, however its not wanting to work for me...
For example if I were to use "3 + 7" as a query, it should execute the query as if it were part of the php code and return "3 + 7 = 10". Im getting nothing for $val at the moment though.
Code: Select all
function execute($query) {
$query2 = addslashes($query).";";
$val = eval($query2);
return $query." = ".$val;
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
What's with the call to addslashes()?
The call to eval() should have "return" in it to receive the output of the expression.
The call to eval() should have "return" in it to receive the output of the expression.