Req #81522 [Sus->Nab]: Switch...case as an expression

From: Date: Tue, 12 Oct 2021 19:46:52 +0000
Subject: Req #81522 [Sus->Nab]: Switch...case as an expression
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237173@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81522&edit=1 ID: 81522 Updated by: requinix@php.net Reported by: kalonya at outlook dot com Summary: Switch...case as an expression -Status: Suspended +Status: Not a bug Type: Feature/Change Request Package: *General Issues Operating System: Irrelevant PHP Version: Irrelevant Block user comment: N Private report: N New Comment: I completely forgot about match! https://www.php.net/manual/en/control-structures.match.php Available since 8.0. Previous Comments: ------------------------------------------------------------------------ [2021-10-12 19:03:05] 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 18:55:21] 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 . "."; ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81522&edit=1

« previous php.bugs (#237173) next »