Jump to content

Stuck on return()


fortnox007

Recommended Posts

Hi all,  I am kinda stuck on the return() statement in the php.net manual about control structures. (http://au.php.net/manual/en/function.return.php)

 

All I see is notes on when not to use it and some scary examples with loads of foo and bar  :o but, it's hard to find out when it would be nice to use it. At the moment i am thinking its another way of echo, but I bet that's wrong. If someone could enlighten me a bit on when return() is useful I would be very happy since i see it more often everyday.

CHeers!

Link to comment
Share on other sites

I use return to 'return' data from a function... For example;

 

I have a function that gets data from a database.. and that function is actually being called by another function that expects to get data from the first function. So the first function that gets the data, has to 'return' the data to the other function. Its very different than echo. See my example below.

 

	function count_orders(){

		$data = $this->db->count_all('orders');

		return $data;

	}

   function view(){
   
   		//load the model
   		$this->load->model('orders_model');
   
   	        //count the results
   	        $count = $this->orders_model->count_orders();

                print_r($count);
}

 

 

Link to comment
Share on other sites

Hi all,  I am kinda stuck on the return() statement in the php.net manual about control structures. (http://au.php.net/manual/en/function.return.php)

 

All I see is notes on when not to use it and some scary examples with loads of foo and bar  :o but, it's hard to find out when it would be nice to use it. At the moment i am thinking its another way of echo, but I bet that's wrong. If someone could enlighten me a bit on when return() is useful I would be very happy since i see it more often everyday.

CHeers!

 

Well, like the manual states, it's primarily used when you want to return a generated value from a function back to the controlling scope (e.g., the code that invoked the function) to be used in its logic.  Canned example:

 

function calculateTotal($arrayOfValues)
{
   $total = 0;

   foreach($arrayOfValues as $value)
   {
      $total += $value;
   }

   return $total;
}

$somePrices = array(2.99, 19.99, 14.99);

$myTotal = calculateTotal($somePrices);

echo "Total cost is: $myTotal";

Link to comment
Share on other sites

Thanks both for the swift replies,

I think I have a slight idea what is meant with it now. I think I have to look a little longer at it to fully let my brains get used to the way of writing. I never used the -> before  ::)

 

But tomorrow I am going to try and learn objects and classes and test this return thing. And i probably than understand the scripts in full detail. Thanks again for explaining it to me, I am not a native speaker so the language used at php.net is sometimes a bit overwhelming  :o

 

TY!

Link to comment
Share on other sites

I'll add that it is also used to simply end a function and does not have to return a value. In fact, I would say that is it's primary function with return-ing a value as a secondary. For example, let's say you have a function that processes an array or records. The records are sorted according to a date value (low to high) and you only want to process the records that are on or before the current date. The function might look something like this:

function processOrder($dataArray)
{
    $today = time();
    foreach($dataArray as $record)
    {
        if($record['timestamp']>$today)
        {
            return; //Exit function
        }
        //Code to process record goes here
    }
}

Link to comment
Share on other sites

Thanks both for the swift replies,

I think I have a slight idea what is meant with it now. I think I have to look a little longer at it to fully let my brains get used to the way of writing. I never used the -> before  ::)

 

But tomorrow I am going to try and learn objects and classes and test this return thing. And i probably than understand the scripts in full detail. Thanks again for explaining it to me, I am not a native speaker so the language used at php.net is sometimes a bit overwhelming  :o

 

TY!

 

Just to clarify, the -> has nothing to do with return at all.  It's an operator used in OOP code.

Link to comment
Share on other sites

Thanks both for the swift replies,

I think I have a slight idea what is meant with it now. I think I have to look a little longer at it to fully let my brains get used to the way of writing. I never used the -> before  ::)

 

But tomorrow I am going to try and learn objects and classes and test this return thing. And i probably than understand the scripts in full detail. Thanks again for explaining it to me, I am not a native speaker so the language used at php.net is sometimes a bit overwhelming  :o

 

TY!

 

Just to clarify, the -> has nothing to do with return at all.  It's an operator used in OOP code.

 

Sorry for over complicating that.. the other examples are much easier to understand...

Link to comment
Share on other sites

Hehe i almost thought I got it, but i might be wrong. After I saw the first 2 examples I thought the return() statement gives the value within the function back to the function. So in example 1 return() gives the value back to count_orders(), and in example 2 return() gives back the value to calculateTotal() so that value can be used elsewhere.

 

Hmm it looks like this is happening to example 3 too, but now it's used to break out of the code? Isnt break than better to use? I just learned that hehe so i thought i just say it :)

Link to comment
Share on other sites

Return doesn't give the value back to the function.  In my example, $total doesn't go back to the function calculateTotal().  It goes to the code that invoked calculateTotal().  This value is stored by the variable $myTotal in the global scope.  If I didn't put the assignment statement there, the value would still be returned, but would essentially disappear as it wasn't stored or used in any way.

 

Return always leaves a function with a value when it's called.  If a value or variable is specified (again, like $total in my example), that's the value/variable that leaves the function.  If no value or variable is given, NULL is sent by default.  That's what happens in example 3.

Link to comment
Share on other sites

Return doesn't give the value back to the function.  In my example, $total doesn't go back to the function calculateTotal().  It goes to the code that invoked calculateTotal().  This value is stored by the variable $myTotal in the global scope.  If I didn't put the assignment statement there, the value would still be returned, but would essentially disappear as it wasn't stored or used in any way.

 

Return always leaves a function with a value when it's called.  If a value or variable is specified (again, like $total in my example), that's the value/variable that leaves the function.  If no value or variable is given, NULL is sent by default.  That's what happens in example 3.

 

Ah now I see it, and understand why in your example your second code has an array, (because the function is written for arrays. sorry for my ignorance php is my first programming language  :-[

Thanks  a lot for the help!

Link to comment
Share on other sites

Thanks both for the swift replies,

I think I have a slight idea what is meant with it now. I think I have to look a little longer at it to fully let my brains get used to the way of writing. I never used the -> before  ::)

 

But tomorrow I am going to try and learn objects and classes and test this return thing. And i probably than understand the scripts in full detail. Thanks again for explaining it to me, I am not a native speaker so the language used at php.net is sometimes a bit overwhelming  :o

 

TY!

 

I'm not sure what your native language is, but the php.net manual lists these languages: Brazilian Portuguese, French, German, Japanese, Polish, Romanian, Persian, Spanish, Turkish, and I'm sure there are resources in some other languages.

Link to comment
Share on other sites

I'm not sure what your native language is, but the php.net manual lists these languages: Brazilian Portuguese, French, German, Japanese, Polish, Romanian, Persian, Spanish, Turkish, and I'm sure there are resources in some other languages.

You're totally true : ) I should have looked further than php.net (my language isn't between them) but I thought I run through the manual because everyone keeps saying  :rtfm: lols :)

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.