Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Functions Function Returns and More Returning Values

return statements, how many

I remember reading in Programming Logic & Design: Introductory by Farrell, which is sort of a general book about no language in particular, that there should be only one return statement in a function. Not just one return value, but only one return statement. This was emphasized in the flowcharts (which were tedious)

Would this then be preferable? Hampton Paulk

function hello($name){
  if($name == 'Beyoncé'){

    $message = 'Hello, Beyoncé';
  }
  else {
    $message = "hello, stranger"; 
  }
  return $message;
}

3 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

It's true that you can't return more than once in a function, but in the case you have posted here, it's okay to have multiple return statements if they are inside if ... else statements. I suppose it could become more confusing if you have a lot of nested if ... else statements (or switch statements), in which case, your posted method is probably preferable.

Ryan absolutely has the right of it, I just wanted to add 2 cents. I think the author may have been trying to help you avoid the common mistake of using multiple return statements and expecting both values to be returned, as well as the terrible code smell that can come from overcomplicated methods like Ryan pointed out. There is no magic number of branched return statements to verify, but if a method has more than a few branches and returns, it may be time to rethink how that method is designed.

As an application design principle, I would say having a single point of exit can make the code substantially more readable and understandable, but if it makes the most sense to use two return statements nested inside control structures, I say go for it!

Refactoring a method down to it's simplest will make the code much easier to maintain and extend. I looked at your example and personally thought it could be whittled down even further:

<?php
function hello($name){
    if($name != 'Beyoncé'){
        $name = 'stranger';
    }
    return "Hello, " . $name;
}

Thank you Joe, I do see yours is even more preferable. By the way, how you make your code colorful? I pasted mine and you see it's all grey

Thank you Ryan, I probably misunderstood what the book author was saying. I'm glad the above is ok.

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

No problem! I am by no means a PHP pro, so take my advice with a grain of salt (or the whole container)! ;)