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

Tawny Bartlett
Tawny Bartlett
24,674 Points

Best practice with php functions... is it best to use return or echo?

I haven't written a lot of PHP from scratch, but I have seen other peoples' functions and was wondering what's the best practice when writing functions... to echo the output in the function, or to return the value and then echo it?

Say I was writing a function to build products into a HTML carousel for best sellers. Should I use echo, or should I use return? I understand returning a variable means you can reuse the variable etc, but ... what do you usually do? If I don't NEED to use the variable again, should I just echo it? Or is it beneficial for me to always return it? Is there any difference in resource usage or anything?

Sorry if I sound noobish...! Thank you for your advice.

Adam Young
Adam Young
15,791 Points

I think that while every project is different, future changes are always something to consider.

In the example, it may be fine to echo the results of the function, but what if you decide that you want to store that information in a database later? Or use that result as part of a conditional? A return gives you the flexibility to echo the results, or to use them in any other fashion.

As far as resource usage, you're not going to see much difference imo, or at least not enough for performance to be a consideration in making this choice.

I personally prefer a return in almost all situations.

Tawny Bartlett
Tawny Bartlett
24,674 Points

Hmm, thank you for your valuable advice! :) That's fantastic, so I suppose there's no "wrong" way. I think I will favour the return method just in case... Hopefully it will become more clear the more I do functions!!

1 Answer

If you're really wanting to follow some guidelines, you should check out PSR-1 from php-fig.

If you haven't heard of php-fig, they are a body that establish and maintain standards of coding for php - essentially they're writers of a rule book that many people follow.

You can learn a lot more about php-fig and various psr-x's in this course on Treehouse.

psr-1 says:

Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.

A very basic interpretation - there should be a separation between 'things that do/change stuff' and 'things that work stuff out'. If you have a file of functions/class that is responsible for creating output, it might be necessary for functions in this file to echo stuff. But that same group of functions that create output shouldn't be working out what they're going to show.

E.g. imagine a search results page. One function might work out and return all the information a user has asked for. A second function might be responsible for displaying that data to the page.

Obviously you could do all this inside one function, but as Adam Young mentioned, you're looking for as much flexibility as possible when you code. I find asking myself "Is it easy to reuse this?" a good code-check. Say you were implementing AJAX or wanted the same search data but presented differently on another page - with this separation, it becomes incredibly easy to chat to your application and ask for bitesize chunks of information without worrying about any 'side-effects'.

It's very easy to catch onto how this separation works in MVC frameworks. Treehouse has a beginner course to laravel but I would advise only starting this after you've been through an introduction to Object Oriented Programming.

Also "Sorry if I sound noobish...! Thank you for your advice." - Asking questions to learn more is awesome. Please don't apologise for asking questions, no matter how 'beginner-ish' you think they sound!

OฤŸulcan Girginc
OฤŸulcan Girginc
24,848 Points

Just to be clear, you are saying (or php-fig.); it is better to use return function, because of its flexibility. Did I understand that correctly?

(Edit: Oops, I just saw Adam's comment! So, if I answer my own question, the answer will be yes. :))