5

I am trying to add a condition dynamically in the if condition . But it is not working . Please help me to fix the issue.

I am trying a code like this

 $day_difference = "some integer value";

  if(sheduled_time == 'evening'){
        $condition = '>';
    }else{
      $condition = '==';
    }

then

if($day_difference.$condition.  0){ 
  echo "something";       
 }else{
   echo "h";
 }
Anish
  • 4,262
  • 6
  • 36
  • 58

5 Answers5

6

An alternative to gerald's solution; I would suggest that you use a function that validates the inputs using a switch-case operation:

function evaluate ($var1, $operator, $var2)
{
    switch $operator
    {
         case: '<': return ($var1 < $var2);
         case: '>': return ($var1 > $var2);
         case: '==': return ($var1 == $var2);
    }
    return null;
}
Daniel
  • 3,726
  • 4
  • 26
  • 49
5

What you need is the eval() method.

I.e.

$var1 = 11;
$var2 = 110;
$cond1 = '$var1 > $var2';
$cond2 = '$var1 < $var2';

if(eval("return $cond1;")){
   echo $cond1;
}

if(eval("return $cond2;")){
   echo $cond2;
}

As justly noted beneath, you should exercise the necessary precautions when using this method!

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
  • 4
    `The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.` – Ron van der Heijden Aug 24 '12 at 13:56
  • Please be aware that this is very dangerous. – Daniel Aug 24 '12 at 13:56
  • `If eval() is the answer, you're almost certainly asking the wrong question` -Rasmus Lerdorf – gen_Eric Aug 24 '12 at 13:58
  • You're very right! Just tried to literally answer the question. Added the warning in my answer. – Gerald Versluis Aug 24 '12 at 14:02
3

This is not the way to do this.
Just define a function which returns true if the desired conditions are met.
For example, we can define the function decide which receives two arguments, $day_difference and $scheduled_time:

function decide($day_difference, $scheduled_time)
{
    if($scheduled_time == 'evening')
    {
        return $day_difference > 0;
    }
    else
    {
        return $day_difference == 0;
    }
}

And use it like so:

if( decide($day_difference, $scheduled_time) )
{ 
    echo "something";       
}
else
{
    echo "h";
}
EyalAr
  • 3,160
  • 1
  • 22
  • 30
1

according to your requirements this can be done using the PHP eval() function which i don't recommend using it only when necessary.

you can check When is eval evil in php?

you can use the below script instead:

if(  $sheduled_time == 'evening' && $diff > 0 )
{
    echo "This is the Evening and the Difference is Positive";
}
else if($diff == 0)
{
    echo "This is not evening";
}
Community
  • 1
  • 1
amd
  • 20,637
  • 6
  • 49
  • 67
0

Thankyou for helping me solve my question

I solved this in another way

 $day_difference = "some integer value";

 $var1 = false ;
if($sheduled_time == 'evening_before'){ 
    if($day_difference > 0 ){ 
        $var1  = true ;
    }
}else{
    if($day_difference == 0 ){ 
        $var1  = true ;
    }
}

if($var1 === true){ 
  echo "something";       
}else{
  echo "h";
}
Anish
  • 4,262
  • 6
  • 36
  • 58