Jump to content

multiple if statements and an else... displaying else.


87dave87

Recommended Posts

What am I doing wrong with this code?  When the if statement is used it still displays the else <title> too.

[code]
<? 
$url = $_SERVER['REQUEST_URI'];

if($url == '/')
{
echo "<title>emulators.cc - ";
echo $num_rows;
echo " console and handheld emulators for Windows, Mac and Linux computers";
echo "</title>";
}
if(strpos($url, "about")!== false) 
{
echo "<title>emulators.cc > About Us</title>";
}
if(strpos($url, "search")!== false) 
{
echo "<title>emulators.cc > Search results for ";
echo $_POST['emusearch'];
echo " (";
echo $num_rows;
echo ")";
echo "</title>";
}
if(strpos($url, "windows")!== false) 
{
echo "<title>emulators.cc > Windows Emulators > ";
echo $platform['platform'];
echo " (";
echo $num_rows;
echo ")";
echo "</title>";
}
if(strpos($url, "mac")!== false)
{
echo "<title>emulators.cc > Mac Emulators > ";
echo $platform['platform'];
echo " (";
echo $num_rows;
echo ")";
echo "</title>";
}
if(strpos($url, "linux")!== false)
{
echo "<title>emulators.cc > Linux Emulators > ";
echo $platform['platform'];
echo " (";
echo $num_rows;
echo ")";
echo "</title>";
}
else
{
echo "<title>emulators.cc -  console and handheld emulators for Windows, Mac and Linux computers</title>";
}
?>
[/code]
As you have it, the "else" clause only pertains to the last "if", so it will always be invoked unless the word "linux" is in the variable "$url".

What you need to do is change all "if" statements, except the first to "elseif" statements, then it should work as you expect.

Ken
perhaps you might be better using the switch - it will be mreo efficient.

[code]
switch (true)
{
case preg_match('/about/', $url):
  break;
case preg_match('/search/', $url):
  break;
...
ad nausea
...
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.