Switch statement: (PHP tutorials – part-5)

Written by Saiful Islam

What is switch statement?

The switch statement is applied for producing several cases. Cases are executed automatically when the value within the switch statement becomes true for any case. Actually, the switch works as the association of if statements because it executes the program if the condition becomes true for any case.

Basic structure:

  • Now let’s have a look at the following examples and try to determine the structure and condition behind it:



  1. The value of the purchase is “car” which is similar with the textual value of case ”car”. So, only the commands of this case will be executed.
  2. After executing all commands under the appropriate case (case “car”) the whole program will stop executing because of having the “ break; “ statement the last.
  3. If no case match with the value of $purchase variable then the program under default will be executed.

So, the output will be:


switch statement in php

  • If we use a numeric value in stead of texts:



  1. The value of x is equal to 2 which is appropriate for the case2. It is the 2nd case and the value of x is also 2 and so only the 2nd case will be executed.

Then the program will stop executing because of the break statement.


  switch statement in phpusing switch statement in php

About the author

Saiful Islam

Leave a Comment