simple if/else php quesiton

He has: 688 posts

Joined: Feb 2001

This code works perfectly. The only part you need to worry about it the if/else formatting for two options.

<?php
if(preg_match(\"/domain1\.com/i\", $_SERVER[\"HTTP_HOST\"])) {
  include(\"/home/user1/public_html/header.php\");
} else {
  include(\"/home/user2/public_html/header.php\");
}
?>

But when I tried to make it an if/if/else with a third option if got all sortsa screwed up.

<?php
if(preg_match(\"/domain1\.com/i\", $_SERVER[\"HTTP_HOST\"])) {
  include(\"/home/user1/public_html/header.php\");
} else {
if(preg_match(\"/domain2\.com/i\",
$_SERVER[\"HTTP_HOST\"])) {
  include(\"/home/user2/public_html/header.php\");
} else {
  include(\"/home/user3/public_html/header.php\");
}
?>

So what did I do wrong here with the ifs and elses and bracket thingys? Thanks.

demonhale's picture

He has: 3,278 posts

Joined: May 2005

put the else tag inside the ifs???

He has: 698 posts

Joined: Jul 2005

I think this is what you want. Combine the middle else/if to become an elseif statement. Smiling

<?
if(preg_match("/domain1\.com/i", $_SERVER["HTTP_HOST"])) {
  include("/home/user1/public_html/header.php");
}
  else if(preg_match("/domain2\.com/i", $_SERVER["HTTP_HOST"])) {
  include("/home/user2/public_html/header.php");
} else {
  include("/home/user3/public_html/header.php");
}
?>
'

(EDIT: Fixed spacing...)

Kurtis

He has: 688 posts

Joined: Feb 2001

Yes, that did it. Thanks!!! Smiling
(note: I don't know if it was necessary but I put a space between "else if")

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

fifeclub wrote: (note: I don't know if it was necessary but I put a space between "else if")

....

Quote: quoted from http://us2.php.net/manual/en/control-structures.elseif.php

In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 698 posts

Joined: Jul 2005

yeah, that was a screw up. Sticking out tongue

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

I know you've already changed it, but you were missing a bracket. If you'd tacked on one more closing bracket at the end to finish off the first else, that would have fixed it as well.

You had this:

if (...)
{...}
else
{
if (...)
{...}
else
{...}
'
...which should have been this...
[code]
if (...)
{...}
else
{
if (...)
{...}
else
{...}
}
'

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

This is where good indenting would work.

<?php
if(preg_match(\"/domain1\.com/i\", $_SERVER[\"HTTP_HOST\"])) {
   include(\"/home/user1/public_html/header.php\");
} else {
   if(preg_match(\"/domain2\.com/i\",
$_SERVER[\"HTTP_HOST\"])) {
     include(\"/home/user2/public_html/header.php\");
   } else {
     include(\"/home/user3/public_html/header.php\");
   }

?>

This way you can easily see something is missing.

I love a good editor that will do (, [, else { if(preg_match(\"/domain2\.com/i\", $_SERVER[\"HTTP_HOST\"])) { include(\"/home/user2/public_html/header.php\"); } else { include(\"/home/user3/public_html/header.php\"); } } [/codefilter_php]or if they all are "one liners" as I call them:

<?php
if(preg_match(\"/domain1\.com/i\", $_SERVER[\"HTTP_HOST\"]))
   include(\"/home/user1/public_html/header.php\");
else
   if(preg_match(\"/domain2\.com/i\",
$_SERVER[\"HTTP_HOST\"]))
     include(\"/home/user2/public_html/header.php\");
   else
     include(\"/home/user3/public_html/header.php\");


?>
This way requires good formatting and like I said, I usually only use it for all one liners, even if one section is more than one line, then I will bracket them all for clarity.

-Greg

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.