dpub33855 2014-11-17 18:54
浏览 20
已采纳

用于检测服务器环境的多个switch语句的语法[重复]

This question already has an answer here:

I need to detect different server environments in my site's config file. Up until now I was fine with detecting them using only the server's address.

switch ( $_SERVER['SERVER_ADDR']){
    case '127.0.0.1':
    // stuff
    break;
    case '111.222.333.444';
    // stuff
    break;
}

But I now need to test my environment against both the SERVER_ADDR and SERVER_NAME. I'm no php'er, so I've had a stab at

switch ( $_SERVER['SERVER_ADDR'] && $_SERVER['SERVER_NAME'] ){
    case ('127.0.0.1','local'):
    // stuff
    break;
    case ('111.222.333.444','gimmesomefunk.com');
    // stuff
    break;
}

But it's obviously wrong. Any clues?

</div>
  • 写回答

3条回答 默认 最新

  • douyan1927 2014-11-17 18:58
    关注

    If you want to keep using switch, you'll have to combine both to one variable like this:

    switch (array($_SERVER['SERVER_ADDR'], $_SERVER['SERVER_NAME'])){
        case array('127.0.0.1', 'local'):
        // stuff
        break;
        case array('111.222.333.444', 'gimmesomefunk.com'):
        // stuff
        break;
    }
    

    However, the more normal way would be to use if, elseif:

    if ( $_SERVER['SERVER_ADDR'] == '127.0.0.1' && $_SERVER['SERVER_NAME'] == 'local' ) {
        // stuff
    } elseif ( $_SERVER['SERVER_ADDR'] == '111.222.333.444' && $_SERVER['SERVER_NAME'] == 'gimmesomefunk.com' ) {
        // stuff
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历