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

Why can't we echo a function & why can we only echo a variable?

If we want to echo the contents of a function, why is PHP not set up to allow us to simply echo the contents of a function directly? Why do we need a variable to play kind of a "middle man" role?

2 Answers

Actually, we don't need a "middle man" variable.

This code is completely valid:

Notice that I'm not very familiar with PHP, but I'm pretty sure this is valid. If I'm wrong somewhere in my code please tell me!

<?php

function return64 () {
  return "Sixty Four, my favorite number!";
}

echo return64();

?>

Hi, as I understand, functions are used to use a certain code over several times. But they do so only when they are called. When we use echo, we display immediately the content of the function. With return, we store the content and display it once the function call is being echoed or printed.