Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:16:49+00:00 2026-06-12T16:16:49+00:00

I have often been told that I should not use multiple return points, instead

  • 0

I have often been told that I should not use multiple return points, instead I should just use one:

take for example this function;

function data($item){
    switch($item){
        case 'one':
            return 1;
        case 'two':
            return 2;
        case 'three':
            return 3;
        case 'different_type':
            return 'Something Different';
        default:
            return false;
    }
}

Apparently a better way of writing this would be;

function data($item){
    $value = false;
    switch($item){
        case 'one':
            $value = 1;
            break;
        case 'two':
            $value = 2;
            break;
        case 'three':
            $value = 3;
            break;
        case 'different_type':
            $value =  'Something Different';
            break;
        default:
            $value = false;
    }
    return $value;
}

Is there any reason that is not a matter of preference for one over the other?

I imagine that the second one gives some IDE’s a better chance at type hinting the return value? but are there performance issues here as well?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:16:50+00:00Added an answer on June 12, 2026 at 4:16 pm

    Is there any reason that is not a matter of preference for one over the other?

    Sometimes but that depends on concrete code.

    I imagine that the second one gives some IDE’s a better chance at type hinting the return value?

    No, that is normally not the case.

    But are there performance issues here as well?

    Early returns can shortcut longer paths in the code so can have a benefit.

    A good coding guideline does normally not govern this strictly nowadays, in earlier times with languages not that flexible it might have made sense to keep a strict approach (last line of a function must be the single return command).

    Nowadays it is known that it is more important to reduce Cyclomatic Complexity which is often the case with returning early. However, take this with a grain of salt, it’s not that if you return early ever, that this is automatically the case.


    As you’re speaking about code, the first example should be in my eyes:

    function data($item) {
    
        static $map = [
            'one'   => 1,
            'two'   => 2,
            'three' => 3,
            'different_type'
                    => 'Something Different',
        ];
    
        # return @$map[$item] ?: false;
        return isset($map[$item])
            ? $map[$item] 
            : false
            ; 
    }
    

    But this would also run counter your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been told and watched others be told very often: do not use
I have been inspecting some PHP source codes and I more often than not
I have been messing around with recursion today. Often a programming technique that is
THis is something that I have been experiencing often. I am running a PHP
I have the often discussed drawRect not getting called. I've been through many posts,
Sorry, that I have been posting here so often -- I have been doing
I have been coming across these two words more often but i didn't see
I have been looking around for solutions, and tried to implement what is often
I've been using debug() more often now, but sometimes I wonder which functions have
I have often PATHs for files which do not exist in my codes. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.