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

5
  • Why do you need to do this? There's an answer, but it's best avoided Commented Jul 26, 2013 at 15:28
  • try put dot : $_GET[$param] . $compare . $param_val Commented Jul 26, 2013 at 15:29
  • possible duplicate of Dynamic Comparison Operators in PHP Commented Jul 26, 2013 at 15:30
  • 2
    @tonoslfx That will just perform a boolean check on the string "my_parameter >= 5" rather than actually evaluate it Commented Jul 26, 2013 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 :) Commented Jul 26, 2013 at 15:30

5 Answers 5

3
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

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

2 Comments

My answer was similar - but yours is cleaner - provided he doesn't need the if statement - perhaps just add in if (comparer($param, $compare, $param_val)) {...
@RocketHazmat: except I don't mind having several exit points. ;p
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;
}

1 Comment

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

Look at eval()

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

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

3 Comments

Please don't suggest eval without explaining the security issues
Using eval with $_GET is just asking for trouble. What if I send $_GET['val'] = "unlink('/var/www/index.php')"? ;-P
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 :)
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
}

Comments

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.

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.