php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81522 Switch...case as an expression
Submitted: 2021-10-12 18:55 UTC Modified: 2021-10-12 19:46 UTC
From: kalonya at outlook dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kalonya at outlook dot com
New email:
PHP Version: OS:

 

 [2021-10-12 18:55 UTC] kalonya at outlook dot com
Description:
------------
I don't know if this is the right place but I'd like to be able to use switch...case like Java. Without using "break" and assigning the returned value to a variable directly.

Test script:
---------------
<?php

$day = 2;

$today = switch($day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    case 4 -> "Thursday";
    case 5 -> "Friday";
    case 6 -> "Saturday";
    case 7 -> "Sunday";
    default -> "Out of day range!";
}

echo "Today is: " . $today . ".";


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-12 19:03 UTC] requinix@php.net
-Summary: Switch...case feature request +Summary: Switch...case as an expression -Status: Open +Status: Suspended -Package: *Programming Data Structures +Package: *General Issues
 [2021-10-12 19:03 UTC] requinix@php.net
You're asking for switch to be possible as an expression rather than only as a statement. This is not a simple change to the language and so would require using the RFC process.
https://wiki.php.net/rfc/howto

But you can already do something simple like this using arrays.
https://3v4l.org/K3DAL
 [2021-10-12 19:46 UTC] requinix@php.net
-Status: Suspended +Status: Not a bug
 [2021-10-12 19:46 UTC] requinix@php.net
I completely forgot about match!
https://www.php.net/manual/en/control-structures.match.php

Available since 8.0.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 13:01:34 2025 UTC