Automatic Switching

Discussion in 'PHP' started by logohits, Jan 16, 2011.

  1. #1
    I like to use a php function to switch between ON and OFF automatically upon the call.
    Of course something simple like

    $condition="OFF";
    switch ($condition) {
    case "OFF" :
    $condition="ON";
    break;
    case "ON" :
    $condition="OFF";
    break;
    }

    this always returns ON but that is not what I need. How can I make it so that it remember the previous setting of $condition and act accordingly so it alternate between ON/Off automatically upon the call to the function?

    Thanks
     
    logohits, Jan 16, 2011 IP
  2. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe you can store the last value (ON or OFF) in a session variable.

    
    session_start();
    
    $_SESSION['last_state'] = 'OFF';
    
    switch ($_SESSION['last_state']) {
    case "OFF" :
    $_SESSION['last_state']="ON";
    break;
    case "ON" :
    $_SESSION['last_state']="OFF";
    break;
    }
    
    PHP:
     
    mnvlxxx, Jan 16, 2011 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    GOD O GOD Why SWITCH FOR ONLY 2 PARAMETERS?

    $_SESSION['last_state'] = ($_SESSION['last_state'] == 'OFF') ? 'ON' : 'OFF';

    To easy :p
     
    EricBruggema, Jan 18, 2011 IP