Jump to content

do return values always need to be "catched" into a variable?


eldan88

Recommended Posts

Hey,

 

I wanted to know if there is any difference between catching a return value and assigning into a variable, and just catching the value when when you just simply call the function on its own.

 

Here is what I mean below

 

In the first example I called the return by simple calling the function. In the second example I catched the return value through a variable, and then echoed it out.

 

<?php
// Example 1 
function text (){

$string = "this is a string </br>";
return $string;}

echo text();

// Example 2
function text (){

$string = "this is a string </br>";
return $string;

}

$new_value = text();
echo $new_value;

}
?>

Hi,

Storing the return data into a variable is useful when you want to use again, or manipulate that data in your code.

For example, calling a function three times requires more memory than storing the value in a variable and using the variable.

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.