D Rice
D Rice

Reputation: 71

Declaring the variable used in PHP Switch Statements

I have an old script that uses the variable $action and then the switch statement. My problem is that in PHP 5. 7 I must declare the variable before it is used - so what do I declare the value to be for a variable that switches?

if(isset($pwd) && ($action == "login") &&
    ($pwd == $admin_password)) 
{
    $admintest = 1;
    $cookie_value = base64_encode("jmkads:$pwd");
    // 86400 secs is 24 hours
    setcookie("jmkads",$cookie_value, time()+86400);
}
else if(isset($jmkads)) {
    $cookie_value = base64_decode($jmkads);
    $cookie_value = explode(":", $cookie_value);
    if(($cookie_value[0] == "jmkads") && 
        ($cookie_value[1] == $admin_password)) 
    {
        $admintest = 1;
    }
}

if(!$admintest) {
    Login_Page();
    exit;
}

$db = connect_to_db();
if($db == 0) {
    echo "Unable to connect to database, check if the MySQL".
        " server is active and the settings of ad_config.php".
        " are correct.\n";
}
else {
    switch($action) {
        case "add_client":
            Page_Header("Add Client");
            add_client();
            break;
        case "add_client2":
            Page_Header("Add Client");
            insert_client_data();
            break;

and so on and so forth (there are many options) - I just wanted to show enough of the script so it was clear.

Upvotes: 0

Views: 181

Answers (1)

Guillaume Giraudon
Guillaume Giraudon

Reputation: 26

In your case, that variable would be declared as a string since your cases are matching strings.

Upvotes: 1

Related Questions