Tim Herbert
Tim Herbert

Reputation: 120

PHP Switch with simple strings not working

$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.

Upvotes: 1

Views: 1155

Answers (1)

J_D
J_D

Reputation: 681

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.

Upvotes: 3

Related Questions