Jump to content

Switch Statement


Gex

Recommended Posts

Now this is something that has me bugged for a while. What exactly is the difference between a Switch statement and the If..Else statement. I mean they both in general do the same thing. Is there something that the Switch Statement can do or is faster than the If..Else. Sorry I am rather moderate at PHP I know the stuff, but something's just seem rather mysterious.ThanksGex

Link to comment
Share on other sites

An if statement lets you branch in two ways (or more, if you use elseif), a switch statement can have many branches.If you're going to compare one value against another value - an if statement should be used. If you will be comparing one value and performing different actions based on several possible values, a switch statement should be used.Examples:If statement:

if ($a==$b) $a=0; else $b=0;

Equivalent switch statement:

switch ($a){  case $b:	$a=0;	break;  default:	$b=0;}

In the above example, the if statement is simpler and easier to understand. More complex example:

if ($a==$b) $b=0; elseif ($a==$c) $c=0;elseif ($a==$d) $d=0;else $a=0;

switch ($a){  case $b:	$b=0;	break;  case $c:	$c=0;	break;  case $d:	$d=0;	break;  default:	$a=0;}

The switch statement is easier to read, especially in situations where you have a lot of cases.

Link to comment
Share on other sites

An if statement lets you branch in two ways (or more, if you use elseif), a switch statement can have many branches.If you're going to compare one value against another value - an if statement should be used. If you will be comparing one value and performing different actions based on several possible values, a switch statement should be used.Examples:If statement:
if ($a==$b) $a=0; else $b=0;

Equivalent switch statement:

switch ($a){  case $b:	$a=0;	break;  default:	$b=0;}

In the above example, the if statement is simpler and easier to understand. More complex example:

if ($a==$b) $b=0; elseif ($a==$c) $c=0;elseif ($a==$d) $d=0;else $a=0;

switch ($a){  case $b:	$b=0;	break;  case $c:	$c=0;	break;  case $d:	$d=0;	break;  default:	$a=0;}

The switch statement is easier to read, especially in situations where you have a lot of cases.

Ahh I see, so if its a simple system where I wish to compare small data between each other it is best to you If .. Else while if I am planning on comparing alot of data I use a Switch. Thanks for clearing that up for me.
Link to comment
Share on other sites

Ahh I see, so if its a simple system where I wish to compare small data between each other it is best to you If .. Else while if I am planning on comparing alot of data I use a Switch. Thanks for clearing that up for me.
eh, sort of...an if else statement can give you more control. I would use a switch if plan on having a few specific cases, but if else's can do a range i.e. if(x > 0 && x < 10), etc. It really depends on the situation. You wouldn't want to have to write a switch statement for 0, 1, 2, 3, 4, 5,......
Link to comment
Share on other sites

eh, sort of...an if else statement can give you more control. I would use a switch if plan on having a few specific cases, but if else's can do a range i.e. if(x > 0 && x < 10), etc. It really depends on the situation. You wouldn't want to have to write a switch statement for 0, 1, 2, 3, 4, 5,......
Ahh I see what you mean. Thanks to both of you. This forum is really useful, especially for finding out the important parts needed. Cheers.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...