blueplasma_dev
blueplasma_dev

Reputation: 35

Using variables in if statements

PHP novice here. I searched for this, but i'm sure i'm not using the right syntax regarding my issue. Apologies then if this is a duplicate:

I have these 3 variables:

$param = get_sub_field('custom_parameter');
$compare = get_sub_field('parameter_compare');
$param_val = get_sub_field('parameter_value');

each one would return this:

$param is 'my_parameter'
$compare is either '==', '<=', or '=>'
$param_val is something like '5' or any value that the user sets

What i have is an editing interface where the user can set their parameter name, set the compare and then add the value. To that they can also add an action that occurs if the parameter matches. I'm using this in conjunction with $_GET.

What i'd like to do is insert each variable from above into my if statement so the comparison is created by the user. However, it keeps giving me an error when i try to do this:

if($_GET[$param] $compare $param_val) {
// do something
}

The error i get is:

Parse error: syntax error, unexpected T_VARIABLE

This of course works just fine:

if($_GET[$param] == $param_val) {
// do something
}

Hopefully i've explained this well enough and any help is greatly appreciated.

Update: Thank you for answering this for me and jumping on it so quickly. Learned a ton here!!

Upvotes: 1

Views: 257

Answers (5)

Brad Christie
Brad Christie

Reputation: 101604

function comparer($param, $compare, $param_val)
{
  switch ($compare){
    case '==': return $param == $param_val;
    case '!=': return $param != $param_val;
    case '<=': return $param <= $param_val;
    case '>=': return $param >= $param_val;
    case '<':  return $param <  $param_val;
    case '>':  return $param >  $param_val;
    default: return FALSE;
  }
}

/* ... */

if (comparer($param, $compare, $param_val)){
  // true
}

Very simple method to get you going. I would, at all costs, resist the temptation to use eval, unless you want to invest a lot of time in sanitizing those three parameters.

Oh, and an example

Upvotes: 3

martincarlin87
martincarlin87

Reputation: 11042

Hmm interesting, I think I'd approach it like this (untested):

function comparison($param, $compare, $param_val) {
    if ($compare == '==') {
        if ($param == $param_val) {
            return true
        }
    }
    if ($compare == '<=') {
        if ($param <= $param_val) {
            return true
        }
    }
    if ($compare == '>=') {
        if ($param >= $param_val) {
            return true
        }
    }
}

Not very efficient or DRY, could probably use a switch as that would probably be better but this was the first thing to pop into my head.

Usage

if (comparison($param, $compare, $param_val)) {
    echo 'it's true';
} else {
    echo 'it's false';
}  

Edit

As per usual, I have been beaten to the punch by better code :)

P.S. I'm not sure why you have $param and then use $_GET[$param] so I've just used $param in my answer.

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227240

The best way would be to make a function for this. Have that function uses a switch to determine the operator, then return the comparison.

function compare($a, $b, $operator){
    $ret = NULL;
    switch($operator){
        case '==':
            $ret = $a == $b;
            break;
        case '>=':
            $ret = $a >= $b;
            break;
        case '<=':
            $ret = $a <= $b;
            break;
    }
    return $ret
}

Then just simply call it:

if(compare($_GET[$param], $param_val, $compare)){
    // do something
}

Upvotes: 1

mikevoermans
mikevoermans

Reputation: 4007

I think I would use a switch statement to avoid any scary eval code.

Such as:

switch($compare) {
    case '==':
        if($_GET[$param] == $param_val) {
            // do something
        }
    break;
    case '<=':
        if($_GET[$param] <= $param_val) {
            // do something
        }
    break;
    case '>=':
        if($_GET[$param] <= $param_val) {
            // do something
        }
    break;
}

Upvotes: 1

Daniele Vrut
Daniele Vrut

Reputation: 2873

Look at eval()

http://php.net/manual/en/function.eval.php

With this you can parse a code you format in a string.

Upvotes: 1

Related Questions