Everything PHP: Echo versus Return

Posted on January 20th, 2009

Everything PHP

In PHP, when you’re handling data, there are two ways you can send it. One is to send it to the browser, and the other is to pass it off to another variable or function. We’re going to talk about these differences today.

Echo

Echo is a language construct which takes the provided argument(s) and then outputs them to the requesting browser. Which looks like this:

[code='php']echo 'This string of text will be outputted to the browser.';[/code]

Remember, always end it with a semi-colon. It is also possible to provided multiple parameters to echo, and it will output them all:

[code='php']echo 'This string will be outputted,', $variable, 'and so will that variable.';[/code]

Which can also be achieved using periods instead of commas.

You should note that there’s also print, which does the same thing as echo, except can only accept one parameter, and always returns 1.

Return

Return is also a language construct, but it is used to pass a value off to a variable or another function. Calling return in your script or function will either end the script or end the function and then return the value. Return is often used in functions to pass on the value without outputting it to the browser, or in files that are intended to be included, to, again, pass on the value without outputting it to the browser. Let’s look at an example of a function passing off data to a variable:

[code='php']function return_example( $arg ) {
if ( $arg == 'banana' ) {
return 'Banana';
}
else {
// There's nothing else to do, so end execution of this function
return;
}
}

$fruit = return_example( 'banana' );[/code]

In this example, the value of $fruit will become “Banana”, as that what the function will send off. Also notice the empty return, which can be used to end the execution of the function or file if there’s nothing else that needs to be done in it.

You can also use a function to return a success or failure (true or false):

[code='php']function return_example2() {
if ( 1+1 == 2 ) {
return TRUE;
}
else {
return FALSE;
}
}

if ( return_example2() == TRUE ) {
echo 'All is right with the world.';
}
else {
echo 'Something has defiantly gone wrong...';
}[/code]

In this example, 1 + 1 will always be true, so that’s what we return, which is then checked by an if. It is worth noting that in the if you can also use 1 for true and 0 for false.

In closing

So, basically you’ll want to use echo whenever you want to output something to the browser, and use return when you want to end the script or function and pass on data to another part of your script.

Posted in Development | | | Digg This | del.icio.us | Technorati


Related Topics:
Everything PHP: Alternative Syntax for Control Structures
Everything PHP: Functions
Everything PHP: If, Else and Elseif
Everything PHP: Switch
Everything PHP: Constants and Globals

RSS feed

2 Comments

Gravatar
Comment by Funny Stuff
2009-01-21 08:07:47

I don’t think I ever use print instead of echo. I’ve been using echo every time since I started learning PHP. Although I use periods instead of commas when outputting variables.

Gravatar
Comment by Matt Freedman ~ come follow me on twitter @mattfreedman
2009-01-21 16:49:30

Print does have its uses, as it acts more like a function, in that it will return a value of 1. This has its (occasional) uses.

However, if you have no uses for that, echo should be used, as it is (very marginally) faster than print.

 
 

Sorry, the comment form is closed at this time.

© 2007 BlueFur Hosting | Privacy Policy
Theme by Unique Blog Designs