-2

I want to check different conditions into if statement based on different scenario (Will get the $status value as 'Y' or 'N'). Please check the code below and let me know the issue here.

$fstrto = "10"; 
$cstrto = "7";
if($status == 'N')
{
    $cond = "$fstrto <= $cstrto";
}
else
{
    $cond = "$fstrto >= $cstrto";    
}
if($cond)
{
    echo "Success";
}

This code is not working as it takes the "$fstrto <= $cstrto" as variable.

FirstOne
  • 6,033
  • 7
  • 26
  • 45
Nuju
  • 322
  • 6
  • 17

5 Answers5

2

Remove the quotes. Use intval/doubleval if the input is a string as in $fstrto = intval($fstrto);.

$fstrto = 10; 
$cstrto = 7;
if($status == 'N')
{
    $cond = $fstrto <= $cstrto;
}
else
{
    $cond = $fstrto >= $cstrto;    
}
if($cond)
{
    echo "Success";
}

Why it works: $cond is being assigned the value of a boolean expression, the values of which can be true or false. if($cond) just checks whether $cond is true or false

Adder
  • 5,708
  • 1
  • 28
  • 56
1

what is need to do is when using string as a php code use

eval — Evaluate a string as PHP code

Use below code work like charm:

    $fstrto = "10"; 
    $cstrto = "7";
    if($status == 'N')
    {
    $cond = "$fstrto <= $cstrto";
    }
    else
    {
    $cond = "$fstrto >= $cstrto";    
    }
    if(eval("return $cond;"))
    {
    echo "Success";
    }

IMPORTANT:

Use of eval is highly discouraged

NEVER EVER use eval with params by POST/GET without sanitize them

When is eval evil in php?

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
  • 2
    If you're going to mention `eval()`, you should also mention why accepting user input into that is a horrible idea - just in case OP wants to use it for something that could be exploited – Qirel Jun 08 '17 at 11:03
0

You "$fstrto <= $cstrto" is a string now a compare statement.

$fstrto = "10"; 
$cstrto = "7";
if( ($status == 'N' && $fstrto <= $cstrto) || ($status != 'N' && $fstrto >= $cstrto) )
{
  echo "Success";
}
LF00
  • 27,015
  • 29
  • 156
  • 295
0

Potentially turn it into a function that funnels into a switch statement like so:

function evaluateCondition($status, $a, $b) {
    switch ($status) {
        case 'Y':
            return $a >= $b;
            break;
        case 'N':
            return $a <= $b;
            break;
        default:
            // Error Log. Unknown Status.
    }
}

Any future addition can be appended onto the switch statement as necessary, if it gets more convoluted have each case return a separate function() to improve readability.

In terms of the current version you could use it like so:

$result = evaluateCondition('Y', 5, 6);
var_dump($result); // bool(false)

Hope that helps.

Mikey
  • 2,606
  • 1
  • 12
  • 20
-2
$fstrto = "10"; 
$cstrto = "7";
$cond = false;
if($status == 'N')
{
    if($fstrto <= $cstrto){
        $cond = true;
    }

}
else
{
    if($fstrto >= $cstrto){
        $cond = false;
    }
}
if($cond)
{
echo "Success";
}
Kruti Aparnathi
  • 175
  • 1
  • 11
  • I think `$cond` should be `true` if `$fstrto >= $cstrto`... Also, I'd define `$cond` with default value of `false` to prevent warnings. – FirstOne Jun 08 '17 at 10:58
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 08 '17 at 16:58