Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Trying to write a php if else statement along the lines of:

if ($_SERVER['SERVER_NAME'] = local ) {

Content here should display if the current url is for example, local.test.com

} elseif ($_SERVER['SERVER_NAME'] = stage)

Content here should display if the current url is for example, stage.test.com

} else {

Content here should display if the current url is for example, test.com

}

I'm assuming ($_SERVER['SERVER_NAME'] = local ) and ($_SERVER['SERVER_NAME'] = stage) isn't right so hopefully someone knows the exact syntax. Thanks!

share|improve this question

closed as off topic by Ladadadada, Michael Hampton, mdpc, Chopper3, Iain Oct 23 '12 at 20:33

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Your question is off topic for Serverfault because it appears to relate programming. It may be on topic for StackOverflow but please search their site for similar questions that may already have the answer you're looking for. –  Iain Oct 23 '12 at 20:33

1 Answer 1

You can do it with a switch statement and use the default case to do that :-) http://php.net/manual/en/control-structures.switch.php

I'm not sure if you did that on purpose but you need a double "equal" sign to test the value and you also need to put your value between quotes :

if ($_SERVER['SERVER_NAME'] == 'local' ) {....
share|improve this answer
    
Great, I'll add the extra = and the quotes. Thanks! –  cchiera Oct 23 '12 at 15:18
    
Actually, it seems that doesn't quite work. If I change local to local.test.com it works but it doesn't if it's just local. –  cchiera Oct 23 '12 at 15:23
    
That's normal, the values must be exactly the same! You can write a regular expression to detect that have a look on php.net php.net/manual/en/function.preg-match.php –  DJYod Oct 23 '12 at 19:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.