0

I would like to build an IF statement in PHP using some options that are coming from the Database.

Let's say I get an array like this from the DB:

$db = [
    'number1' => [
        '<= 5',
        '&&'
    ],
    'number2' => [
        '> 200',
        '&&'
    ],
    'number3' => [
        '= 1',
        '||'
    ],
    'number3' => [
        '= 2',
        '||'
    ],
];

I'd like to translate it into this:

if ($data['number1'] <= 5 && 
    $data['number2'] > 200 &&
    ($data['number3'] == 1 || 
     $data['number3'] == 2
    )) {
    // do something
}

Of course, I'd start it by doing a foreach and putting the KEYs of the array into the $data[''] array. However, I'm pretty unsure how can I put the logical operators and everything else in place.

Can anyone give me an idea please?

4
  • 1
    It becomes complex as you can mix and and or logic, what if you would like brackets to say x and (y or z). Commented May 9, 2018 at 16:19
  • Are you sure about that starting structure? You can't have two of the same key (number3). Commented May 9, 2018 at 16:35
  • @NigelRen Yes exactly it gets pretty complicated, that's why I was wondering how people usually solve this issue. Commented May 9, 2018 at 16:44
  • @Don'tPanic Actually that's by design, but you're right it's not looking ok inside an array. I just wanted to make a point that I need a solution that covers the case when there are 2 of the same conditions with different values. Commented May 9, 2018 at 16:45

2 Answers 2

5

It is always better to sanitize data that comes from MySQL or any user produced data, instead of using eval(). For your case, I would suggest having your data split into:

  • Operator
  • Operand
  • Next Logic

And then compare it with the existing ones and then use that. To start with, you can do:

if ($operator == ">=")
  if ($operand >= $value)

Or something similar to build it recursively - this is the key. Hope this helps.

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

Comments

1

Well, sounds quite strange, but I've tried small code example: https://3v4l.org/h21gv

<?php

$arr = [
    'number1' => [
        'value' => 2,
        'comparator' => '<=',
        'compareTo' => 5,
        'next' => '&&'
    ],
    'number2' => [
        'value' => 230,
        'comparator' => '>',
        'compareTo' => 200,
        'next' => '&&'
    ],
    'number3' => [
        'value' => 1,
        'comparator' => '==',
        'compareTo' => 1,
        'next' => '||'
    ],
    'number3' => [
        'value' => 2,
        'comparator' => '==',
        'compareTo' => 2,
        'next' => '||'
    ],
];



function comparator(array $ar) {
    $i = 0;
    $m = count($ar);
    $last = true;
    foreach ($ar as $compare) {
        $i++;
        switch($compare['comparator']) {
            case '<=':
                $bool = $compare['value'] <= $compare['compareTo'];
                break;
            case '>':
                $bool = $compare['value'] > $compare['compareTo'];
                break;
            case '==':
                $bool = $compare['value'] == $compare['compareTo'];
                break;
        }
        if ($i < $m) {
            switch($compare['next']) {
            case '||':
                $last = $last || $bool;
                break;
            case '&&':
                $last = $last && $bool;
                break;
            }
        }
    }
    return $last;
}

var_dump(comparator($arr));

2 Comments

The 'comparator' in your last part of your test array has only 1 '='
@NigelRen thanks, had a typo also in switch, didnt notice this one

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.