Jump to content

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.

Edited by Jessica

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.

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.