2

I am stuck on something in PHP

    $l = $par['Length'];
    $w = $par['Width'];
    $d = $par['Depth'];
    $length = $par['Expr_1'];
    $width = $par['Expr_2'];

$par is an array read from a database. Length, Width, Depth are numbers Expr_1 & Expr_2 are formulas stored as strings ie something like this:

($l + .0625) * ($w + .125) * ($l + .125) * ($w + .0625) + 1.625

What should happen is that $l $w should substitute with the values. If I echo $l,$w,$d independently they have the right values. If I echo $length or $width it shows the formula.

Even when I try eval("$width = {$par['Expr_2']};"); it doesn't force it to calculate instead of just read the formula. I know the formula is right because if I manually stick it into $length or $width it works fine.

2
  • 1. It is bad to use eval. 2. If you still want to use it, make sure it is enabled from php.ini. Commented Jan 29, 2012 at 6:47
  • 1. don't care as that portion of the database that provides the formulas will be read only so not worried about dangerous code as I check the imputed variables. 2. I just looked through the full php.ini and didn't see anything about enabling or disabling eval. please specify where I should be looking Commented Jan 29, 2012 at 7:03

4 Answers 4

1

Instead of using eval for the formulas... there is the possible option of using something like preg_replace() instead... to just replace those formula variables coming from the database... and then converting/typecasting the replaced result to an (int) or whatever data type you need this to be. Then computing the formula from there. Though... it might not be as reliable as eval.

http://php.net/manual/en/function.preg-replace.php

Sign up to request clarification or add additional context in comments.

2 Comments

wouldn't I just have the same problem? would php evaluate it at that point?
I think @Amber's way of storing the formulas in the database would work better. This is just another way to look at the problem...
1

Variable interpolation into strings only happens when those strings are evaluated source code. When they're just values stored in variables, assignments don't do interpolation.


Better would be to store a simple parseable format that doesn't require full-blown eval(). Here's some code that can store a format that looks like this:

L,0.5;W,0.2;D,0.3:4.7

which would be equivalent to (L+0.5)*(W+0.2)*(D+0.3)+4.7:

$eqn_parts = explode(':', $input);
$add_part = $eqn_parts[1];
$mult_parts = explode(';', $eqn_parts[0]);

$accum = 1;
foreach($mult_parts as $part) {
    $bits = explode(',', $part);
    switch($bits[0]) {
        case "L":
            $accum *= ($length + (int)$bits[1]); break;
        case "W":
            $accum *= ($width + (int)$bits[1]); break;
        case "D":
            $accum *= ($depth + (int)$bits[1]); break;
    }
}
$accum += (int)$add_part;

4 Comments

so how do I fix? I need to store 50+ formulas and the user needs to enter the lxwxd to be converted to a lxw
Consider storing the equation in a way that you can parse and evaluate instead - for instance, store (L+0.5)*(W+0.25)+0.3 as L,0.5;W,0.25:0.3 (Which would then be interpreted as "split on the :, take everything before the colon and split it on ;s, each of those items is a comma-separated pair of an input type and an added constant. Take those, multiply them together, then add the constant after the colon.)
so there is no way to force php to evaluate it as source code? Did I have the eval command wrong?
I am confused because if you do $a = 5; $b = 6; $c=$a +$b; $c would evaluate as 11. is $c not a string before the values of $a and $b are declared. Sorry had to edit b/c it is 2am here and I was confused
0

Although amber's solution would have worked, I wanted to keep it simple and make all the work I did entering the formulas into the db not a waste. I simply combined $length / $width at the beginning of the string and : at the end and eval'd it.

Thanks, everyone

EDIT to add code that I used:

 $l = $par['Length'];
    $w = $par['Width'];
    $d = $par['Depth'];
    $par['Expr_1'] = '$length='.$par['Expr_1'].';';
    eval($par['Expr_1']);
    $par['Expr_2'] = '$width='.$par['Expr_2'].';';
    eval($par['Expr_2']);

1 Comment

Edit your question instead of posting an answer.
0

Assuming that you will not be passing "any user provided data into it without properly validating it beforehand."

The following seems to work well.

$width = $par['Expr_2'];
eval('$width = '.$width .';');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.