Incorrect string length of $_SERVER['SERVER_NAME']
There is a nice explanation here https://github.com/bdkjones/CodeKit/issues/440 which ends with
In short, there's nothing wrong here --> MAMP has the correct Host header.
Since you are trying to distinguish between test.local and localhost, you could use:
if (strpos($_SERVER['HOST_NAME'],'localhost') !== false) { echo 'localhost';} else { echo 'not localhost';}