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;

}
?>

Link to comment
Share on other sites

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.

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.