87dave87 Posted November 21, 2006 Share Posted November 21, 2006 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] Link to comment https://forums.phpfreaks.com/topic/27964-multiple-if-statements-and-an-else-displaying-else/ Share on other sites More sharing options...
trq Posted November 21, 2006 Share Posted November 21, 2006 You shoul be using if elseif else. With a series of elseifs. Link to comment https://forums.phpfreaks.com/topic/27964-multiple-if-statements-and-an-else-displaying-else/#findComment-127916 Share on other sites More sharing options...
kenrbnsn Posted November 21, 2006 Share Posted November 21, 2006 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 Link to comment https://forums.phpfreaks.com/topic/27964-multiple-if-statements-and-an-else-displaying-else/#findComment-127920 Share on other sites More sharing options...
87dave87 Posted November 21, 2006 Author Share Posted November 21, 2006 thanks! Link to comment https://forums.phpfreaks.com/topic/27964-multiple-if-statements-and-an-else-displaying-else/#findComment-127922 Share on other sites More sharing options...
ToonMariner Posted November 21, 2006 Share Posted November 21, 2006 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] Link to comment https://forums.phpfreaks.com/topic/27964-multiple-if-statements-and-an-else-displaying-else/#findComment-127926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.