douyonglang4845 2016-12-28 02:04
浏览 471
已采纳

PHP Switch使用简单的字符串无效

$url = $_SERVER['REQUEST_URI'];
$url = basename($url).PHP_EOL;

switch ($url) {
    case 'digital-marketing-and-seo':
        $new_url = "Digital Marketing & SEO";
        break;
    case 'websites-digital-destinations':
        $new_url = "Websites & Digital Destinations";
        break;
    case 'brand-identity-evolution':
        $new_url = "Brand Identity & Evolution";
        break;
    case 'strategy-consulting':
        $new_url = "Strategy & Consulting";
        break;
    case 'government-universities':
        $new_url = "Government & Universities";
        break;
    case 'hospitality-travel':
        $new_url = "Hospitality & Travel";
        break;
    case 'architecture-engineering':
        $new_url = "Architecture & Engineering";
        break;
    case 'wine-spirits':
        $new_url = "Wine & Spirits";
        break;
    default:
        $new_url = '';
        echo 'default derp';
}

So I have this block of code and it will not work for me. The url can say exactly what is in the case and it will default. I'm trying to get url structures to build out a shortcode and I need it to match what is entered.

I determined that if I enter just add a variable for a string for $url = 'brand-identity-evolution'; The switch works. I made sure it's a string with is_string function and can't figure out why this isn't working.

  • 写回答

1条回答 默认 最新

  • douwei6478 2016-12-28 02:06
    关注

    You added a PHP_EOL at the end of the URL, so all of your cases will fail because you're not checking for that. Remove your PHP_EOL and you'll be fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥15 MATLAB数据处理插值
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的
  • ¥15 硬件IIC从模式的数据发送,中断数据的接收,不能用HAL库(按照时序图)