All Syllabus

Home About PHP HTML CSS SQL Java Script jQuery AJAX      

PHP Tutorial

PHP Introduction

PHP 5 Installation

PHP 5 Syntax

PHP 5 Variables

PHP 5 echo and print Statements

PHP 5 Data Types

PHP 5 String Functions

PHP 5 Constants

PHP Arithmetic Operators

PHP 5 if...else...elseif Statements

PHP 5 switch Statement

PHP 5 while Loops

PHP 5 for Loops

PHP 5 Functions

PHP 5 Arrays

PHP 5 Sorting Arrays

PHP 5 Superglobals

PHP Forms

PHP 5 Form Handling

PHP 5 Form Validation

PHP 5 Forms - Required Fields

PHP 5 Forms E-mail /URL

PHP Form Complete

PHP Advanced

PHP 5 Multidimensional Arrays

PHP 5 Date and Time

PHP Include Files

PHP File Handling

PHP File Open/Read

PHP File Create/Write

PHP File Upload

PHP Cookies

PHP Sessions

PHP E-mail

PHP Secure E-mails

PHP Error Handling

PHP Exception Handling

PHP Filter

PHP Database

PHP MySQL Introduction

PHP MySQL Connect

PHP Create DB/Tables

PHP MySQL Insert Into

PHP MySQL Select


PHP Order By

PHP Update

PHP Delete



PHP 5 switch Statement

The switch statement is used to perform different actions based on different conditions.

The PHP switch Statement

Use the switch statement to select one of many blocks of code to be executed.


switch (n) {
  case label1:
     code to be executed if n=label1;
  case label2:
     code to be executed if n=label2;
  case label3:
     code to be executed if n=label3;
     code to be executed if n is different from all labels;

This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.



switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
  case "blue":
    echo "Your favorite color is blue!";
  case "green":
    echo "Your favorite color is green!";
    echo "Your favorite color is neither red, blue, or green!";