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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 14:01:30 2024 UTC