simple if/else php quesiton
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 posted this at 02:29 — 20th October 2005.
He has: 3,278 posts
Joined: May 2005
put the else tag inside the ifs???
kazimmerman posted this at 02:38 — 20th October 2005.
He has: 698 posts
Joined: Jul 2005
I think this is what you want. Combine the middle else/if to become an elseif statement.
<?
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
fifeclub posted this at 02:55 — 20th October 2005.
He has: 688 posts
Joined: Feb 2001
Yes, that did it. Thanks!!!
(note: I don't know if it was necessary but I put a space between "else if")
Mark Hensler posted this at 15:09 — 28th October 2005.
He has: 4,048 posts
Joined: Aug 2000
....
Mark Hensler
If there is no answer on Google, then there is no question.
kazimmerman posted this at 03:10 — 20th October 2005.
He has: 698 posts
Joined: Jul 2005
yeah, that was a screw up.
timjpriebe posted this at 12:53 — 20th October 2005.
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
{...}
}
Tim
http://www.tandswebdesign.com
Greg K posted this at 13:51 — 20th October 2005.
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\");
?>
-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.