douyonglang4845 2016-12-27 18: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-27 18: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部