Case Switch – PHP
switch/case is very similar or an alternative to the if/elseif/else commands. The switch command will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result. I usually use case switch on a PHP page that has multiple options.
For example, lets say I have a set of PHP scripts that will create, edit and delete text links in a MySQL table. Instead of using multiple pages to accomplish this. I can use case switch to call each statement:
switch ($action) {
case 1: //edit link
//edit code would go here
break;
case 2:
//delete code would go here
break;
default:
//default code would go here. As the page already loads what I wanted I would just put echo ""; to print nothing.
}
Next I would need to write links for edit and delete that would call the condition:
links.php?action=1
links.php?action=2
And that is a simple way to use case switch…




