php - in specific condition set and store a variable until another specific condition -


hello i'm pretty new in programming. need solve problem in php solution in different language great. tryied solve if statement if condition changed variable gone. easy example better understanding.

// possible conditions ( 'cond1', 'cond2', 'cond3', 'cond4','cond5' ) // conditions can called randomly 

i have somethng this:

$variable = 'off'; since ( $condition == 'cond2' )     $variable = 'on'; until ( $condition == 'cond4' ) 

the goal switch variable 'on' in 'cond2' condition , hold on when others conditions changing independently on order until condition changed 'cond4' , variable switched 'off'.

thanks suggestions.

i don't think current concept realizable in php cannot listen variables, need actively notified. 1 scenario same solution different concept be

class condition {      private $value;     private $variable = false;      public function setcondition($new_value) {         $this->value = $new_value;     }      public function getcondition() {         return $this->value;     }      public function isvariableset() {         return ($this->variable === true); //true  if $this->variable true                                            //false otherwise     }  } 

now in method setcondition(...) can listen , actively set variable.

    public function setcondition($new_value) {         switch ($new_value) {             case 'cond2':                 $this->variable = true;                 break;             case 'cond4':                 $this->variable = false;                 break;         }         $this->value = $new_value;     } 

with can use following

$foo = new condition(); $foo->setcondition('cond1'); var_dump( $foo->isvariableset() ); //false  $foo->setcondition('cond2'); var_dump( $foo->isvariableset() ); //true  $foo->setcondition('cond3'); var_dump( $foo->isvariableset() ); //true  $foo->setcondition('cond4'); var_dump( $foo->isvariableset() ); //false 

or in case:

$conditions = array( 'cond1', 'cond2', 'cond3', 'cond4','cond5' );  $cond = new condition(); foreach ($conditions $i => $condition) {     $cond->setcondition($condition);     if ($cond->isvariableset() == true) {         $toggle = 'on';     }     else {         $toggle = 'off';     }     $results[$condition] = $toggle.' ;  '; } 

if don't create instance of condition outside loop, gain nothing create new object everytime , no state stays. however, required.

you can via array_map() , save foreach()

$conditions = array( 'cond1', 'cond2', 'cond3', 'cond4','cond5' );  $cond = new condition(); $results = array();  $setcondgetvariable = function($condition) use($cond) {     $cond->setcondition($condition);     if ($cond->isvariableset() == true) {         $toggle = 'on';     }     else {         $toggle = 'off';     }     return $toggle.' ;  '; };  $results = array_map($setcondgetvariable, $conditions); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -