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
https://forums.phpfreaks.com/topic/121484-return-question/
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
https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626488
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
https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626498
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
https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626502
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.