Php switch statement

A switch statement is like a series of if statement. We are comparing a value with many other values.

Pretend we have a script and want to greet our users.


$user_name = 'adam';

switch( $user_name ) {
    case "dave": 
        echo 'Hello Dave.';
    case "dan":
        echo 'Hello Dan.';
    case "bill":
        echo 'Hello Bill.';
    case "adam":
        echo 'Hello Adam.';
        echo 'Hello Guest';

The above example is not the best use for a switch statement, but it gets the point across.

In the past I have used them to read a URL path for information or in functions for processing images based on the file extension.

Read more about the PHP Switch statement.

Signup for my mailing list

Receive other rambings like this on design, code, and some times food.