Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding conditions dynamically in php if condtion

Tags:

php

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";
 }
like image 794
Anish Avatar asked Aug 24 '12 13:08

Anish


1 Answers

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;
}
like image 163
Daniel Avatar answered Nov 09 '22 14:11

Daniel