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!!
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
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;
}
Look at eval()
http://php.net/manual/en/function.eval.php
With this you can parse a code you format in a string.
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
}
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.
Related
I am in the making of some code that needs to check if a users login details are correct, and I therefore need a lot of if-statements inside each other. If any of the conditions in the if-statements are not true, they should alle return the same value. Is there an easy way of doing this, instead of writing the same multiple times? I have made an example below to visualize my problem. As you can see here I write " else { return false; }" multiple time, and this is what I am wondering if you are able to do more efficiently. Maybe so I only have to write "or else return false" once.
//some code
if (/*some condition*/) {
//some code
if (/*some new condition*/) {
//some code
if (/*some new condition*/) {
//some code
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
I am having a hard time finding a good way to explain my problem, so if you have a more elegant way of explaining it, do not hesitate to edit my post. I am also not quite sure that the title is as good as it could be, so if you have any ideas to an alternativ please say so :)
Lets say you have something like that (I added No):
if ( condition1 ) {
//some code 1
if ( condition2 ) {
//some code 2
if ( condition3 ) {
//some code 3
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
Since each time a condition is false, you exit the function returning false, you can directly test if the condition is false using a negation (if the negated condition is true):
if ( !condition1 ) {
return false;
}
//some code 1
if ( !condition2 ) {
return false;
}
//some code 2
if ( !condition3 ) {
return false;
}
//some code 3
This doesn't reduce the number of if statements, but you avoid many nesting levels and the else statements.
You can also try the switch statement. For many situations it will produce cleaner code.
<?php
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
The switch statement is also compatible with using strings:
<?php
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
?>
Good luck! :)
in PHP (and even other languages but in the present case this is more for PHP), i often end up sometimes having to code long conditions such as the example below:
i have a code with many conditions and i want to display a different result based on certain conditions.
if something is RED and the other thing is RED, then print X,
if something is RED but the other thing is BLACK, then print Y
if something RED and the other thing is RED but a third thing is blue, then print X
& so on
is there a way to properly handle this by using some kind of data structure/configuration array/matrix/whatever ? that is, storing these "conditions" and "results" properly in some kind of configuration array or other ?
instead of having to code nested conditions that can be tricky to support afterwards
like this very small example but in a much bigger scale
if (preg_match(/something/, $string) {
$result = 'GREEN';
} elseif (preg_match(/something/, $string) {
$result = 'RED';
} else {
if (something else) {
$result = 'GREEN';
} else {
if (something OR something) {
$result = 'AMBER';
} else {
$result = 'GREEN';
}
}
}
or is it the only way of handling this ?
maybe with a single
if (something and something or something) {
} elseif (something and something and something) {
} elseif (something and something or something and something) {
} etc
thank you for your help
i'm coding an app that should display a different "status" for a certain data depending on many different data (other attributes of this data), and i'd like to avoid having unreadable code
You can use nested arrays:
$conditions = [
'/something1/' => [
'/something2/' => "X"
],
'thing3' => [
'/thing3/' => 'Y',
'/thing4/' => 'Z'
]
];
$matched = false;
foreach ($conditions as $key1 => $val1) {
if (preg_match($key1, $string)) {
if (is_array($val1)) {
foreach ($val1 as $key2 => $val2) {
if (preg_match($key2, $string)) {
$result = $val2;
$matched = true;
break;
}
}
} else {
$result = $val1;
$matched = true;
}
}
if ($matched) {
break;
}
}
if (!$matched) {
$result = 'default';
}
To allow arbitrary levels of nesting you could turn this into a recursive function.
For the purposes of the answer below I'm assuming you're coding OO PHP.
One variable
When I have one variable that determines the outcome, if the variable is boolean or close to being boolean (meaning it can only either be one or two different values), is to use an if() statement like you have. If the variable can be a variety of (known) values, like your colours example, I use a switch() function.
Two or more variables
If I have two variables that determine the outcome, for instance colour and size, I first use a switch(), then for each switch, I use a method that contains another switch().
It may be more verbose, but it is so much easier to keep track of in the long run. Below is a simplified example of the logic.
switch ($colour) {
case 'red':
red_handler($size);
break;
case 'green':
green_handler($size);
break;
case 'blue':
blue_handler($size);
break;
}
/** We already know the first variable value is red */
function red_handler($size)
{
switch ($size) {
case 'small':
echo "my fruit is a cherry";
break;
case 'medium':
echo "my fruit is an apple";
break;
case 'large':
echo "my fruit is a watermelon";
break;
}
}
In a similar vein to #dearsina i'd tend to separate it out into functions for this kind of thing, for example if you know there are lots of cases where it could return the same value, e.g.:
if(isGreen($string)) return 'GREEN';
else if (isRed($string)) return 'RED';
function isGreen($string) {
if(cond1)return true;
if(cond2 && cond3)return true;
if(cond4 || cond 5)return true;
return false;
}
function isRed($string) {
if(cond6)return true;
if(cond1 && cond7)return true;
if(cond2 || cond 8)return true;
return false;
}
we do sometimes use the style you've suggested...
if (something and something or something) {
} else if (something and something and something) {
but you can quickly end up back in the same problems of readability and maintenance issues
Maybe there is a topic about this, but I coudn't find.
I'm storing in a database a comparative operator and a target value, like:
$target_value_1 => 1000
$operator => greather_than_or_equal
Is there a way of using those operators in a variable like:
if ($variable1 $operator $target_value_1){
// Some code
}
Working with PHP and MySQL.
Thanks
I'm not sure what are you building but you can sort this by simply creating a check($operator, $var1, $var2) function along these lines :
function check($operator, $val1, $val2) {
switch ($operator) {
case 'greather_than_or_equal':
return $val1 >= $val2;
break;
case 'greather_than':
return $val1 > $val2;
break;
case 'operator_name':
//return your condition goes here;
break;
default:
return false;
break;
}
}
And use it : if (check($operator, $value1, $value2)) { /* your code */ }
I've got 3 variables:
$left_side = "'Username'";
$equation = "==";
$right_side = "'Username'";
I want to test these variables as it was an if statement like so:
if($left_side $equation $right_side) {
// do something
} else {
// do something else
}
I know this works:
if(eval("return ".$left_side." ".$equation." ".$right_side.";")) {
// do something
} else {
// do something else
}
I always tought it's 'not good' to use eval. Especially when you try to run user input.
Is there an other way to do this? I tried to google it, but it's not my friend to day ;)
You may do something like this:
function testValues($val1, $equation, $val2) {
$res = false;
switch($equation) {
case "==":
$res = $val1 == $val2;
break;
case ">":
$res = $val1 > $val2;
break;
//....
default:
throw new Exception("Unknown operator");
}
return $res;
}
and than use it like:
if(testValues($left_side,$equation,$right_side)) {
//do something
} else {
//do something
}
eval is evil. And no, there's no other (easy) solution, but maybe this one helps:
if ($equation == "==") {
if ($left_side == $right_side) {
// ... your code goes here.
} else {
// Do some other stuff.
}
}
You could use switch:
$left_side = "'Username'";
$equation = "doublequal";
$right_side = "'Username'";
switch($equation){
case 'doublequal':
if ($left_side == $right_side) {
// code
}
break;
//......
}
You should never use eval() especially with user input.
eval() is evil, but call_user_func() is evil too and every framework uses this function in one place or another.
Tools aren't evil. They are just tools. Evil is the way that we use them.
As Uncle Ben said: Great power involves great responsibility :)
I find this trick
http://gonzalo123.com/2012/03/12/how-to-use-eval-without-using-eval-in-php/
the idea is create a temporary file with the PHP source code, include this file with the standard PHP’s include functions and destroy the temporary file.
I have an array of conditions :
$arrConditions = array ('>=2', '==1', '<=10');
...which I want to be able to use in an if...statement.
IE.
if (5 $arrConditions[0])
{
...do something
}
...which would be the same as :
if (5 >= 2)
{
...do something
}
Any help?
Thanks
Such a requirement is a sure sign of a bad design.
Most likely you can do that another, more usual way.
Nevertheless, never use eval for such things.
At least store each operator in pairs - an operator and operand.
$arrConditions = array (
array('>=',2),
array('==',1),
array('<=',10),
);
and then use switch:
list ($operator,$operand) = $arrConditions[0];
switch($operator) {
case '==':
$result = ($input == $operand);
break;
case '>=':
$result = ($input >= $operand);
break;
// and so on
}
But again - most likely you can solve it another, much easier way.
What about this ?
<?php
$arrConditions = array('==2', '==9', '==5', '==1', '==10', '==6', '==7');
$count = 0;
$myval = 0;
foreach ($arrConditions as $cond) {
$str = "if(5 $cond) { return $count;}";
$evalval = eval($str);
if (!empty($evalval)) {
$myval = $count;
}
$count++;
}
switch ($myval) {
case 0: echo '==2 satisfied';
break;
case 1: echo '==9 satisfied';
break;
case 2: echo '==5 satisfied';
break;
case 3: echo '==1 satisfied';
break;
case 4: echo '==10 satisfied';
break;
default : echo 'No condition satisfied';
}
?>