I know the difference between if..elseif and switch statement but I have one confusion When I actually use switch case statement though I can solve it by using if..elseif and else statement.
Switch tests the same variable over and over if elseif allows you to test different variables eg switch($status) { case 'Open': sendOpenEmail(); break; case 'Closed': sendClosedEmail(); break; } if ($status == 'Open') sendOpenEmail(); elseif ($status == 'Closed' && $validEmail && $outcome == 'Accepted') sendClosedEmail(); PHP:
if or else if is the better performance than switch statement. so use if else statement if u have need
both of them has similar work. you can do the same thing using if..else which u can do with switch but not viceversa for complex problems. Switch is not preferred usually by professional programmers, it is actually more compact, quite readable but still lot limited in functionality
they both work similar but have still small but important differences. switch does NOT support multiple conditions if ($var==true and $var2>10) { PHP: The above would not be possible to be easily and cleanly implemented with swutch. also you can use switch to share output between cases swicth ($var1) { case '1': case '3': echo $var1; break; case '2' : echo 'YAY '; case '4': case '5': echo 'YEEHAA'; brerak; } PHP: the above would result in this case 1: 1 case 2: YAY YEEHAA case 3: 3 case 4: YEEHAA case 5: YEEHAA so small differences but these differences decide when it is better to use if elseif or switch hope it helped.