dongmaijie5200 2016-03-17 00:02
浏览 45

声明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.
";
}
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.

  • 写回答

1条回答 默认 最新

  • douwei8096 2016-03-17 00:12
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能