• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php switch case – How to Use Switch…Case Statements in php

php switch case – How to Use Switch…Case Statements in php

March 30, 2022 Leave a Comment

In php, a switch case statement tests a variable against multiple values and when the switch statement finds a match, it executes code in the case block.

$variable = "apple";

switch($variable) {
    case "apple":
        echo "The variable is apple.";
        break;
    case "banana":
        echo "The variable is banana.";
        break;
    default:
        echo "The variable is something else.";
        break;
}

//Output:
The variable is apple.

When working in php, being able to control the flow of data in our programs and perform specific operations based on the values of variables is very important.

When dealing with complex situation in our php programs, we can use switch case statements to control the flow of data based on the value of a variable.

A switch case statement is similar to an if-elseif-else statement and tests a variable against a series of values until it finds a match.

When it finds a match, php executes the block of code corresponding to that match.

The correct syntax for a switch case statement in php is shown below in a simple example.

$variable = "apple";

switch($variable) {
    case "apple":
        echo "The variable is apple.";
        break;
    case "banana":
        echo "The variable is banana.";
        break;
    default:
        echo "The variable is something else.";
}

//Output:
The variable is apple.

php Switch Case Statement Where You Have the Same Value for Multiple Cases

There are some cases when using a switch case statement where you want to take the same action for multiple cases.

In this case, where you want to do the same action for multiple cases, you just need to make sure not to use the ‘break’ keyword. If there is no ‘break’ after a ‘case’, the code will keep executing.

Below is a simple example of showing a switch case statement where you have the same value for multiple cases.

switch ($num) {
    case 0:
    case 1:
    case 2:
        $message = '0-2';
        break;
    case 3:
        $message = '3';
        break;
    default:
        $letter = 'not in 0-3';
        break;
}

Hopefully this article has been useful for you to learn how to create a switch case statement in your php code.

Other Articles You'll Also Like:

  • 1.  Get First Element of Array in php
  • 2.  Remove Duplicates from Array in php with array_unique()
  • 3.  How to Parse URLs with php parse_url() Function
  • 4.  How to Get Session ID in php with session_id()
  • 5.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 6.  php max – Find Maximum Value of Array in php
  • 7.  Add Months to Date in php
  • 8.  Remove Specific Character from String Variable in php
  • 9.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 10.  php preg_match_all – Get All Matches of Pattern in String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy