Brett2KC Posted October 15, 2012 Share Posted October 15, 2012 Hello I am stuck on a problem and I need some help. Basically I need to write a function that takes a reference to an array of numbers, loops over the array and returns the sum of those numbers. I don't exactly understand that or how to do it. Can someone give me a snippet on how it works? Quote Link to comment https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/ Share on other sites More sharing options...
Christian F. Posted October 15, 2012 Share Posted October 15, 2012 The PHP manual for foreach () should give you all the information you need, the rest is pure logic and basic PHP syntax. Quote Link to comment https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/#findComment-1385372 Share on other sites More sharing options...
ManiacDan Posted October 15, 2012 Share Posted October 15, 2012 Is this homework, or can you just use the built in function that already does this? Quote Link to comment https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/#findComment-1385373 Share on other sites More sharing options...
Brett2KC Posted October 15, 2012 Author Share Posted October 15, 2012 Well I need to write the function out, and thats what i dont get. It's not homework, it's just a practice booklet that I am learning on. Quote Link to comment https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/#findComment-1385377 Share on other sites More sharing options...
Jessica Posted October 15, 2012 Share Posted October 15, 2012 (edited) The PHP manual for foreach () should give you all the information you need, the rest is pure logic and basic PHP syntax. If you're trying to learn, and we do it for you, what will you learn? If you get stuck we can help but you haven't shown any code yet. And this sounds like homework. Edited October 15, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/#findComment-1385378 Share on other sites More sharing options...
ManiacDan Posted October 15, 2012 Share Posted October 15, 2012 Accepting a variable "by reference" means putting an ampersand in front of the variable in the function declaration: function myFunction( &$myVariable ) That's a little-used feature which means any changes to $myVariable inside of myFunction() affect the state of the variable OUTSIDE the function, like this: function upper( &$myVariable ) { $myVariable = strtopupper($myVariable); } $a = "abc"; upper($a); echo $a; //prints ABC Foreach is easy, you have the manual for that. Return is also easy, that should be in your book. Quote Link to comment https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/#findComment-1385379 Share on other sites More sharing options...
Barand Posted October 15, 2012 Share Posted October 15, 2012 See array_sum Quote Link to comment https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/#findComment-1385400 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.