fortnox007 Posted September 8, 2010 Share Posted September 8, 2010 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 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! Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/ Share on other sites More sharing options...
petroz Posted September 8, 2010 Share Posted September 8, 2010 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); } Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108929 Share on other sites More sharing options...
KevinM1 Posted September 8, 2010 Share Posted September 8, 2010 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 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"; Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108930 Share on other sites More sharing options...
fortnox007 Posted September 8, 2010 Author Share Posted September 8, 2010 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 TY! Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108933 Share on other sites More sharing options...
Psycho Posted September 8, 2010 Share Posted September 8, 2010 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108936 Share on other sites More sharing options...
KevinM1 Posted September 8, 2010 Share Posted September 8, 2010 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 TY! Just to clarify, the -> has nothing to do with return at all. It's an operator used in OOP code. Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108941 Share on other sites More sharing options...
petroz Posted September 8, 2010 Share Posted September 8, 2010 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 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... Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108942 Share on other sites More sharing options...
fortnox007 Posted September 8, 2010 Author Share Posted September 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108944 Share on other sites More sharing options...
KevinM1 Posted September 8, 2010 Share Posted September 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108953 Share on other sites More sharing options...
fortnox007 Posted September 8, 2010 Author Share Posted September 8, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108956 Share on other sites More sharing options...
AbraCadaver Posted September 8, 2010 Share Posted September 8, 2010 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1108965 Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Author Share Posted September 9, 2010 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 lols Quote Link to comment https://forums.phpfreaks.com/topic/212909-stuck-on-return/#findComment-1109000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.