How do I rewrite the code in order to avoid the repetition of the block case 3:{code block A; break;}?

switch(i) { case 1: {code block A; break;} case 2: {code block b; break;} case 3: {code block A; break;} default: {code block default; break;}}How can I have combined code for case 1 and case 3?

*

*

Trending sort Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

Đang xem: The php switch statement

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
This format is shown in the PHP docs:

switch (i) { case 1: case 3: code block A; break; case 2: code block B; break; default: code block default; break;}With the release of PHP8 and the new match function, it is often a better solution to use match instead of switch.

For the example above, the equivalent with match would be :

$matchResult = match($i) { 1, 3 => // code block A 2 => // code block B default => // code block default}The match statement is shorter, doesn”t require breaks and returns a value so you don”t have to assign a value multiple times.

Xem thêm: Intel Rapid Storage Technology Là Gì ? Intel Rapid Storage Technology Là Gì

Moreover, match will act like it was doing a === instead of a ==. This will probably be subject to discussion but it is what it is.

Share
Improve this answer
Follow
edited Apr 19, 2021 at 9:23
answered Feb 27, 2014 at 16:15

*

B FB F
7,9651414 gold badges4646 silver badges8080 bronze badges
3
Add a comment |
12
Something like this

switch(i) { case 1: case 3: {code block A; break;} case 2: {code block b; break;} default: {code block default; break;}}
Share
Improve this answer
Follow
edited Feb 2, 2020 at 20:49

*

Peter Mortensen
30.4k2121 gold badges102102 silver badges124124 bronze badges
answered Feb 27, 2014 at 16:16

*

marciojcmarciojc
51222 silver badges77 bronze badges
Add a comment |
1
Something like

$i = 10; switch($i){ case $i == 1 || $i > 3: echo “working”; break; case 2: echo “i = 2”; break; default: echo “i = $i”; break; }
Share
Improve this answer
Follow
answered Aug 6, 2018 at 17:06
Hung PhamHung Pham
16711 silver badge33 bronze badges
Add a comment |

Your Answer

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Xem thêm: Tìm Hiểu Hematocrit Là Mạch Gì Và Ý Nghĩa Của Xét Nghiệm Htc

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Submit

Post as a guest

Name
Email Required, but never shown

Post as a guest

Name
Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged php switch-statement or ask your own question.

The Overflow Blog Featured on Meta
Linked
-1
How to have 2 values in switch case
Related
1717
Replacements for switch statement in Python?
702
Multiple cases in switch statement
1081
Why can't variables be declared in a switch statement?
402
Switch statement fallthrough in C#?
1042
Why can't I use switch statement on a String?
2247
How to write a switch statement in Ruby
4960
Reference — What does this symbol mean in PHP?
2227
How does PHP 'foreach' actually work?
1061
Switch statement for multiple cases in JavaScript
408
Using two values for one switch case statement
Hot Network Questions more hot questions

Question feed
Subscribe to RSS
Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-php
Stack Overflow
Products
Company
Stack Exchange Network
Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev2022.9.14.42975

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Related Post

Leave a Reply

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