Jump to content

Referencing An Array Of Numbers


Brett2KC

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/269500-referencing-an-array-of-numbers/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.