1

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!!

  • Why do you need to do this? There's an answer, but it's best avoided – MDEV Jul 26 '13 at 15:28
  • try put dot : $_GET[$param] . $compare . $param_val – tonoslfx Jul 26 '13 at 15:29
  • possible duplicate of [Dynamic Comparison Operators in PHP](http://stackoverflow.com/questions/2919190/dynamic-comparison-operators-in-php) – user1721135 Jul 26 '13 at 15:30
  • 2
    @tonoslfx That will just perform a boolean check on the string "my_parameter >= 5" rather than actually evaluate it – MDEV Jul 26 '13 at 15:30
  • 2
    I have been programming PHP for well over 5 years now and I cannot imagine a situation where a dynamic operator is a good thing. You will have to elaborate. We can give you the answer but that would mean we hate you :) – MonkeyZeus Jul 26 '13 at 15:30

5 Answers5

4
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

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

Look at eval()

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

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

Daniele Vrut
  • 2,835
  • 2
  • 22
  • 32
  • 6
    Please don't suggest `eval` without explaining the security issues – MDEV Jul 26 '13 at 15:31
  • 1
    Using `eval` with `$_GET` is just asking for trouble. What if I send `$_GET['val'] = "unlink('/var/www/index.php')"`? ;-P – gen_Eric Jul 26 '13 at 15:32
  • of course. It's a novice and he must learn all, positive and negative things. I'll never use eval but it's in php lib :) – Daniele Vrut Jul 26 '13 at 15:34
1

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;
}
mikevoermans
  • 3,967
  • 22
  • 27
  • Thank you everyone for jumping on this and for teaching me about the security issues when using eval() with $_GET!!! This works perferctly. You guys are awesome! – blueplasma_dev Jul 26 '13 at 16:10
1

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
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

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.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145