Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convertinf IF -elseif-else to Switch-Case

Status
Not open for further replies.

chipCHE

IS-IT--Management
Aug 5, 2003
15
US
Just wondering if it is possible to convert the following If else statement to a Switch case statement:
<?
if (($Var1 == "XXX") and ($var2 == "24") and (var3 == "36"))
{ $MyAnswer = "AAA"; }
elseif (($Var1 == "YYY") and ($var2 == "24") and (var3 == "36"))
{ $MyAnswer = "BBB"; }
elseif (($Var1 == "ZZZ") and ($var2 == "12") and (var3 == "36"))
{ $MyAnswer = "CCC"; }
else
{

// If none of the above there must be an error
print (" There has been an error (Over Looked Logic case) Please carefully check your selections and try again");
}

?>

Every example of Switch i have ever seen only handles one variable:


Many Thanks.
Regards,
Chip
 
Congratulations on seeing the evil of the elseif statement!

Most examples of using a switch only switch on a single variable because a switch can only take one variable.

Actually, there is no rule that states that the item in the head of the switch must be a variable:

Code:
<?php
$a_array = array (TRUE, FALSE);
$b_array = array (TRUE, FALSE);

foreach ($a_array as $a)
{
        foreach ($b_array as $b)
        {
                switch (TRUE)
                {
                        case ($a && $b):
                                print 'both are true';
                                break;

                        case ($a && !$b):
                                print '$a is true, $b is false.';
                                break;

                        case (!$a && $b):
                                print '$a is false, $b is true';
                                break;

                        case (!$a && !$b):
                                print 'neither is true';
                                break;

                }

                print "\n";
        }
}
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thank you sleipnir214 for taking the time to help me out:)

This is very instructional and gives me a better idea of what can be accomplished.

In my case I have 3 variables each with at least 4 possible values from which to choose a result. To me it appears that it will take less code to keep the if then else statements (Additionally I am very new to programming and thinking of a nest 3 layers deep is a challenge for me).

Would there be a real benefit to converting my code to the switch statement?

Regards,
Chip
 
Well, for starters, a switch statement is easier to read than elseifs. It's easier to get the head around.

If you're thinking of maintainability, then use the switch.

[cheers]
Cheers!
Laura
 
In my opinion, elseif is evil and should never have been included in the language. The construct makes it difficult to maintain code, and there is nothing you can do with an elseif that you can't do more readably using nested if-then-else or switch constructs.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top