Anyone Who Knows PHP Programing

Status
Not open for further replies.

Tigger

Active Member
123
2009
0
0
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
 
7 comments
it's not the proper section to post such a thread just PM a mod to move it to its proper section.
anyway you need to check before putting off into $condition the first time if it's set or not.
if it's not set the put off in it else(it already has a value) then nothing to do.
 
Tried using single quotes?

Strings give a hell lot of trouble when used in switch-case statements. Try

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

If this doesn't work, Then check it via ASCII values(or equivalent).

I hope that helps.
 
you need to store the $condition to session, cookie or database
and the initial value should be retrived from there, it is hard coded in your script...
 
PHP:
session_start();

$status = isset($_SESSION['status']) ? $_SESSION['status'] : "OFF";

switch($status)
{
    case "ON":
         $status = $_SESSION['status'] = "OFF";
    break;
    case "OFF":
         $status = $_SESSION['status'] = "ON";
    break;
}
 
Status
Not open for further replies.
Back
Top