jhodara Posted November 26, 2010 Share Posted November 26, 2010 Hi There, Im trying to figure out how to do this if/else statement. I need it to say, "if this is true, do this. if this is true, do this. If neither is true, do this. So there are three different possible outcomes. I tried to put each new if inside of the previous else. but that doesn't seem to work. $firstname = $_REQUEST['firstname']; $lastname = $_REQUEST['lastname']; if ($firstname == 'Joey' and $lastname == 'Hodara') { $output = 'Welcome to our site, ' . htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') . ', Oh Glorious Leader!'; } else { if ($firstname == 'Matt' and $lastname == 'Fig') { $output = 'Welcome to our site, ' . htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') . ', Portagee!'; } else{ if ($firstname == 'Jon' and $lastname == 'Spense') { $output = 'Welcome to our site, ' . htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') . ', bro!'; } } $output = 'Welcome to our site, ' . htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') . ' ' . htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8') . '!'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/219866-multiple-ifelse-statements/ Share on other sites More sharing options...
revraz Posted November 26, 2010 Share Posted November 26, 2010 If ElseIf Else Quote Link to comment https://forums.phpfreaks.com/topic/219866-multiple-ifelse-statements/#findComment-1139725 Share on other sites More sharing options...
btherl Posted November 26, 2010 Share Posted November 26, 2010 Your original code would work if you add the missing "}" at the end. But I recommend doing as revraz suggested: if (...) { } elseif (...) { } else { } That makes the code more readable. Quote Link to comment https://forums.phpfreaks.com/topic/219866-multiple-ifelse-statements/#findComment-1139789 Share on other sites More sharing options...
aabid Posted November 26, 2010 Share Posted November 26, 2010 You haven't closed first else statement, probably this might be creating problem Quote Link to comment https://forums.phpfreaks.com/topic/219866-multiple-ifelse-statements/#findComment-1139818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.