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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog