Jump to content

Recommended Posts

I'm relatively new to PHP and am currently taking an Intro to PHP class at college and have a question regarding the use of echo versus return in functions.

 

In particular, yesterday I showed my instructor a form I created that included a seperate file which contained all my functions. In some of my functions I need to output html. Needless to say, some of my functions are riddled with echo's. He saw that and mentioned something about "using return instead of echo inside a function" but I dont understand what he meant or how to modify the function to do this.

 

I'm hoping someone here can explain to me how one would output html to the browser from within a function, and maybe take a guess as to what my instructor meant?

 

The main form takes mailing data (to, from, and package size/weight) entered by the user, validates most of it, then displays a summary of what the user entered. The below function is in my includes file and it is the function that displays the summary info. This function was the one my instructor referenced when talking about echo vs. return:

function process_form() {
echo "<b>Sender Details:</b><br />";
echo "<hr />";
echo "     " . $_POST['sender_name']."<br />";
echo "     " . $_POST['sender_address']."<br />";
echo "     " . $_POST['sender_city'].", ".$_POST['sender_state']." ".$_POST['sender_zipcode']."<br />";
echo "<br /><br />";
echo "<b>Recipient Details:</b><br />";
echo "<hr />";
echo "     " . $_POST['recipient_name']."<br />";
echo "     " . $_POST['recipient_address']."<br />";
echo "     " . $_POST['recipient_city'].", ".$_POST['recipient_state']." ".$_POST['recipient_zipcode']."<br />";
echo "<br /><br />";
echo "<b>Package Details:</b><br />";
echo "<hr />";
echo "     Dimensions: " . $GLOBALS['pkgLength'] . "”L x " . $GLOBALS['pkgWidth'] . "”W x " . $GLOBALS['pkgHeight'] . "”H<br />";
echo "     Weight: " . $GLOBALS['pkgWeight']." Lbs.";
echo "<br /><br />";

 

I researched "echo vs. return" online, but all of the examples I saw used the above method for echoing out info... Is the above code syntactically correct? Or how could I improve it?

 

Any help would be greatly appreciated,

Thanks!  ;D

First up, all those echos, take processing time to do. If you were, to say, store all that into a string then return the string and echo the string, that is one echo.

 

Echoing inside functions is fine, especially for simple scripts. However, returning a string will allow you to manipulate that string, so if you wanted to parse it etc you can. And you can call that function anywhere on your script and still be able to echo that string when you want/need to.

 

But yea, 50 echo's like that is not good, you do not need to put an echo on each new line, thank god.

<?php
// I'll show you both ways:
function process_form_echo() {
echo "<b>Sender Details:</b><br />";
echo "<hr />";
echo "     " . $_POST['sender_name']."<br />";
echo "     " . $_POST['sender_address']."<br />";
echo "     " . $_POST['sender_city'].", ".$_POST['sender_state']." ".$_POST['sender_zipcode']."<br />";
echo "<br /><br />";
echo "<b>Recipient Details:</b><br />";
echo "<hr />";
echo "     " . $_POST['recipient_name']."<br />";
echo "     " . $_POST['recipient_address']."<br />";
echo "     " . $_POST['recipient_city'].", ".$_POST['recipient_state']." ".$_POST['recipient_zipcode']."<br />";
echo "<br /><br />";
echo "<b>Package Details:</b><br />";
echo "<hr />";
echo "     Dimensions: " . $GLOBALS['pkgLength'] . "”L x " . $GLOBALS['pkgWidth'] . "”W x " . $GLOBALS['pkgHeight'] . "”H<br />";
echo "     Weight: " . $GLOBALS['pkgWeight']." Lbs.";
echo "<br /><br />";
}
function process_form_return() {
// note you could continue to the variable with:
// $returnVar = "blah
// blah blah blah";
// but I think it's cleaner coding the following way 

// also, be careful with globals. I know they teach that still, but just be careful.

$returnVar = "<b>Sender Details:</b><br />";
$returnVar .= "<hr />";
$returnVar .= "     " . $_POST['sender_name']."<br />";
$returnVar .= "     " . $_POST['sender_address']."<br />";
$returnVar .= "     " . $_POST['sender_city'].", ".$_POST['sender_state']." ".$_POST['sender_zipcode']."<br />";
$returnVar .= "<br /><br />";
$returnVar .= "<b>Recipient Details:</b><br />";
$returnVar .= "<hr />";
$returnVar .= "     " . $_POST['recipient_name']."<br />";
$returnVar .= "     " . $_POST['recipient_address']."<br />";
$returnVar .= "     " . $_POST['recipient_city'].", ".$_POST['recipient_state']." ".$_POST['recipient_zipcode']."<br />";
$returnVar .= "<br /><br />";
$returnVar .= "<b>Package Details:</b><br />";
$returnVar .= "<hr />";
$returnVar .= "     Dimensions: " . $GLOBALS['pkgLength'] . "”L x " . $GLOBALS['pkgWidth'] . "”W x " . $GLOBALS['pkgHeight'] . "”H<br />";
$returnVar .= "     Weight: " . $GLOBALS['pkgWeight']." Lbs.";
$returnVar .= "<br /><br />";
return $returnVar;
}
process_form_echo();
echo '<br />vs:<br />';
$newVar = process_form_return();
echo $newVar;
?>

 

 

Basically, using a return will allow you to further manipulate /format the output before you output it. When just echoing the data, it goes straight to the output buffer, whereas with a return, the function 'returns' a variable. You can then do whatever you please to this variable.

 

Another, maybe better example on why you'd want to do this:

<?php
function math_add_echo($num) {
$num = $num + 2;
echo $num;
}
function math_add_return($num) {
$num = $num + 2;
return $num;
}

// we'll call math_add_echo and cant do anything with the number:
math_add_echo(4);

// but lets say we want to add a few more the result of our function:
$mathVar = math_add_return(4);
$mathVar = $mathVar + 3;
echo $mathVar;
?>

By returning the results from the function, you can do anything with it - echo it, save it to a database, send it in an email, supply it as input to another function, use it in a comparison... You don't need to keep writing a different function or modifying one you already have that is doing the same processing but with a different destination. Just write a function that does the processing you need, return the results, and use it anyway you need at the time. This is basically separating the processing of the data from the presentation of the data and making the function general purpose so that it can be reused in any situation.

Basically, using a return will allow you to further manipulate /format the output before you output it. When just echoing the data, it goes straight to the output buffer, whereas with a return, the function 'returns' a variable. You can then do whatever you please to this variable.

 

Ahh, I see the light now. Thanks for explaining this. It makes sense. By echoing something, once its echoed you cant use it for anything really, accept to just output something to the stdout (screen... sorry, just came from a UNIX class lol). Whereas by putting it into a string and returning it, I could actually use the data/string and manipulate it if needed.

 

Thanks again, I'm going to go play with returns now ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.