Jump to content

Recommended Posts

Hello,

 

I know this is a wage question, but since iam new to the programming world jsut wanted to clarrify this silly doubts.

 

Why do we need return in the function when we can use echo and get the output ?

 

Can someone explain me more abt funtions ?

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/54579-what-is-a-return-in-function/
Share on other sites

basically... it a) makes the function end exactly there... and b) makes the entire function worth that value...

 

if you echo in a function it outputs data...

if you return from a function... $string=function(); you can use that information...

 

i know others may believe differently... but in my "little" world, functions "should" NEVER echo anything... always return... even if its to "echo function();"

 

Hi,

 

i too had the same confusion before,

 

usually function can be called in 2 different ways ( i think )

 

1. just call a function in the code like

 

------- some code -----

//function call, it does something depending on the function

myfunction() ;

----- again some code -------

 

for the above function call, you need not use the return anything, see... you are not assigning any values to a variable, you are just calling a function to so some action there.

 

sample coded for 1

<?php

function myfunction() {

    echo date(time());

}

?>

no returns here

 

2. assign a function output to a variable

 

------ some code ----

// here you are assigning a output of the function

$somevariable = myfunction($parameter);

 

---- again some code-------

 

for this call you MUST use a return to assing the output to the $somevariable.

 

sample code

 

function myfunction($getvalue) {

    $val  = md5($getvalue);

    return $val;

}

 

when you call like this

 

$somevariable = myfunction('abc');

 

and if you echo the code, the value will be a encrypted one, md5 is a type of encryption.... try this with return and without return

 

Regards

Jaikar

 

 

 

 

 

 

i know others may believe differently... but in my "little" world, functions "should" NEVER echo anything... always return... even if its to "echo function();"

 

Agreed. This makes your functions allot more portable. For instance, if you create a function and have it retrieve data from a database then echo html it won't be of any use to you within a cli script.

 

On the other hand, if it simply returns a value, that value can be used on the web, in the cli, where ever.

As stated many times, do not echo in functions, just causes problems, return a string.

 

The purpose of a return function is say that you need a timestamp from a date, but you do not want to print it out as you need to manipulate it and than you need to enter it into a database. Well if strtotime just printed out the timestamp, and if date just echo'ed the date, this would not be possible.

 

<?php
$time = strtotime('07/01/2007');
$new_date = date('Y-m-d', $time); 

$sql = "INSERT INTO db VALUES ('" . $new_date . "');";

echo 'The new date is ' . $new_date;
?>

 

That way it gives you full control on how you handle the timestamp and the way you handle the date once it is retrieved. If those functions were to "ECHO" or "PRINT" data none of that would be nearly as simple as seen above.

 

Anyhow, hope that helps.

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.