Jump to content

return question


Cosizzle

Recommended Posts

Hey,

 

Im playing around with the return method and I'm a little confused. Could someone quickly explain why a return would be used opposed to an echo. Im guessing my confusion might be caused because of the simplicity to my example - a real world situation would help! Thanks

 

Example with return

function add($a,$b) {
$total = $a + $b;
return $total;
}
echo add(1,1);

Output:

2

 

Example with echo

function add($a,$b) {
$total = $a + $b;
echo $total;
}
echo add(1,1);

Output:

2

Link to comment
Share on other sites

Making a function that simply returns a result rather than echoing it is more versatile.

 

For example, you have a function gettitle($id) that returns the name of an article stored in your database. Now, if this function where simply to echo the article title you will have no chance of formatting it. What if you want the article title to appear in all caps? You need to modify the function.

 

If the function simply returns a value, its up to the calling code what to do with that value.

Link to comment
Share on other sites

Hey,

 

Im playing around with the return method and I'm a little confused. Could someone quickly explain why a return would be used opposed to an echo. Im guessing my confusion might be caused because of the simplicity to my example - a real world situation would help! Thanks

 

Example with return

function add($a,$b) {
$total = $a + $b;
return $total;
}
echo add(1,1);

Output:

2

 

Example with echo

function add($a,$b) {
$total = $a + $b;
echo $total;
}
echo add(1,1);

Output:

2

 

You wouldnt need to echo the function in the second example, it is already echoed within the function so to get the output of 2 it would simple be:

 

add(1,1);

 

 

Link to comment
Share on other sites

Right... this is on the tip of my tongue and i (think) just need a small nudge but (sorry for the horrible mistakes) is this thinking in the correct direction.

 

Say I had two files each containing classes, one file executed a query the other ran the query and returned the result.

Query Class (fileA.php)

 

class query {

var $sql = null;

function __construct() {
	$this -> sql = new SQL;
}

function rQuery() {
	$this -> sql -> query("SELECT * FROM table");
	return $this -> sql -> rResult -> result;
}

}

 

 

SQL Class (fileB.php)

class SQL {

var $result = null;

function __construct() {
}

function rResult($query) {
	$result = mysql_query($query) or trigger_error("Query: $query <br/>mysql Error: " . mysql_error());
}

}

Link to comment
Share on other sites

try

<?php
function add_return($a,$b) {
$total = $a + $b;
return $total;
}


function add_echo($a,$b) {
$total = $a + $b;
echo $total;
}

$a = 3; $b = 5;
echo "$a + $b = ". add_return($a, $b). "<hr />\n";
echo "$a + $b = ". add_echo($a, $b). "<hr />\n";
?>

Link to comment
Share on other sites

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.