1

I want to use PHP variables in the formula that came from the database data.

I want to do mathematical logic using a database so users can change the whole logic of output using those variables.

if any clarification is needed you can comment.

thanks in advance :)

<?php
    
    include '../../db.php';
    
    $a = 50;
    $b = 20;
    $c = 30;
    
    $fq = $conn->query("SELECT * FROM `test`");
    $fdata = $fq->fetch_array();
    $formula = $fdata['formula'];
    
    echo $output = $formula;
    
?>

Database table

Database table example

Current Output

$a+$b-$c

Expected Output

40
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Unfortunately you can't store the variables themselves in a database and then later re-use them in your code. You could store the values and the operations in the database. Are the values for $a/$b/$c always the same? – Erik Sep 07 '21 at 09:23
  • actually, those variable data came from post data. I have just created this for sample purposes to explain my issue. is there any other way to get the expected result I have to give that formula building to use level? so that's why I have used a database there. – Vishal Vishwakarma Sep 07 '21 at 09:26
  • 2
    I think this is a duplicate of https://stackoverflow.com/questions/18880772/calculate-math-expression-from-a-string-using-eval – Daniel W. Sep 07 '21 at 09:34
  • @Erik once i run "$result = eval($fdata['formula']);" getting this error. PHP Parse error: syntax error, unexpected end of file in /home/lyfseiwm/pcbmagic.com/test/test/index.php(13) : eval()'d code on line 1 – Vishal Vishwakarma Sep 07 '21 at 09:38
  • @DanielW. yes, it's working. is this support all kinds of operators? – Vishal Vishwakarma Sep 07 '21 at 09:42
  • Does this answer your question? [calculate math expression from a string using eval](https://stackoverflow.com/questions/18880772/calculate-math-expression-from-a-string-using-eval) – CBroe Sep 07 '21 at 10:25

2 Answers2

3

You can use it with eval() but that's a bad idea.

$result = eval($fdata['formula']);

Some servers with shared hosting will prohibit the use of such method and ones like exec().

One clever idea if you have a limited number of operation types ("+", "-", "*", "/") is to explode your string piece by piece and then process them.

A simple example would be

$x = '$a+$b*$c';
$summArray = explode('+', $x);
foreach($summArray as $key => $item) {
    $multiplyArray = ('*', $item);
    if (count($multiplyArray) > 1) {
        $summArray[$key] = $multiplyArray;
    }
}

//now to process
$values = ['$a' => $a, '$b' => $b, '$c' => $c];
$result = 0;
foreach($summArray as $item) {
    if (is_array($item)) {
        $multResult = 1;
        foreach($item as $multItem) {
            $multResult *= $values[$multItem]
        }
        $result += $multResult;
    } else {
        $result += $values[$item];
    }
}

something like that

N69S
  • 16,110
  • 3
  • 22
  • 36
-2
<?php
    
    include '../../db.php';
    
    $a = 70;
    $b = 20;
    $c = 30;
    
    $fq = $conn->query("SELECT * FROM `test`");
    $fdata = $fq->fetch_array();
    $formula = $fdata['formula'];
    
    echo $result = eval('return '.$formula.';');
    
?>

above code will help you in doing the same.

eval('return '.$formula.';')

just need to replace this.

  • 3
    This answer works just fine, but please don't dismiss the other answers telling you to reconsider using "eval", especially if your objective is to allow users to write their own formulas to the database. That leaves you incredibly vulnerable to arbitrary code execution. – Arthur Boucher Sep 07 '21 at 09:58