Jump to content

If, Elseif, but what if you have 5 things to if?


illuz1on

Recommended Posts

I have this piece of code which will show what I intend on doing...

<?
$selection = $_POST["selection"];

if( $_GET["action"] == "clubs" ){
echo "CLUUUBS!";
} elseif( $_GET["action"] == "bars" ){
echo "BARRRS!";	
} elseif( $_GET["lounges"] == "lounges" ){
echo "Lounges!";
} elseif( $_GET["events"] == "events" ){
echo "EVENTS";
}else {
echo "No selection Made";
}
?>

 

How can I make that work? the last 2 if's wont work

 

Thanks guys

Also, a switch() would be more apropos for this situation:

 

switch ($_GET["action"]) {

  case 'clubs':
    echo 'CLUUUBS!';
    break;

  case 'bars':
    echo 'BARRRS!';
    break;

  case 'lounges':
    echo 'Lounges!';
    break;

  case 'events':
    echo 'EVENTS';
    break;

  default:
    echo 'No selection Made';
    break;

}

Switches are nice, but if you don't want to use a switch, I find that a more vertical approach helps...

Not that the above switch would work...

 

<?php
$selection = $_POST["selection"];

if ( $_GET["action"] == "clubs" )
{
echo "CLUUUBS!";
}
elseif ( $_GET["action"] == "bars" )
{
echo "BARRRS!";	
}
elseif ( $_GET["lounges"] == "lounges" )
{
echo "Lounges!";
}
elseif ( $_GET["events"] == "events" )
{
echo "EVENTS";
}
else
{
echo "No selection Made";
}
?>

 

You say the don't work? What aren't they doing?

 

Do you mean when $_GET["action"] equals "clubs", $_GET["lounges"] does NOT equal "lounges"?

That would be because of your logic. If both are true, then only the first will work.

 

If you want all of them to work at a time try this:

<?php
$selection = $_POST["selection"];

if ( $_GET["action"] == "clubs" )
{
echo "CLUUUBS!";
}
elseif ( $_GET["action"] == "bars" )
{
echo "BARRRS!";	
}
else
{
echo "No selection Made";
}

if ( $_GET["lounges"] == "lounges" )
{
echo "Lounges!";
}
else
{
echo "No selection Made";
}

if ( $_GET["events"] == "events" )
{
echo "EVENTS";
}
else
{
echo "No selection Made";
}
?>

 

That would work kinda funny though...

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.