Skip to main content
Added warning
Source Link
Spoody
  • 2.9k
  • 1
  • 27
  • 36

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);
    
    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];
    
    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}

Be aware that this will get sub in sub.test.domain.com, so this is not the best way to parse a URL, only use this if you are 100% sure that the input is going to be sub.domain.com or this won't work.

The ?? is the null coalescing operator it was introduced in PHP 7

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);
    
    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];
    
    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}

Be aware that this will get sub in sub.test.domain.com.

The ?? is the null coalescing operator it was introduced in PHP 7

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);
    
    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];
    
    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}

Be aware that this will get sub in sub.test.domain.com, so this is not the best way to parse a URL, only use this if you are 100% sure that the input is going to be sub.domain.com or this won't work.

The ?? is the null coalescing operator it was introduced in PHP 7

added 257 characters in body
Source Link
Spoody
  • 2.9k
  • 1
  • 27
  • 36

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);
    
    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];
    
    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}

Be aware that this will get sub in sub.test.domain.com.

The ?? is the null coalescing operator it was introduced in PHP 7

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);
    
    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];
    
    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);
    
    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];
    
    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}

Be aware that this will get sub in sub.test.domain.com.

The ?? is the null coalescing operator it was introduced in PHP 7

Source Link
Spoody
  • 2.9k
  • 1
  • 27
  • 36

In my opinion an explode with an array is way shorter and cleaner

private function getAppId($url)
{        
    $subdomain = explode('.', $url);
    
    $ids = [
        'test1' => 2,
        'test2' => 3,
        'test3' => 4,
        'test4' => 5,
    ];
    
    // Return the ID if it exists or 1
    return $ids[$subdomain[0]] ?? 1;
}