Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialErnst van Megen
3,541 PointsI'm stuck at functions
I have re-watched the video 5x. Googled 'php function' a couple of times but for some reason I am doing something wrong.
I have added the following code:
<code> function array_sum();
echo $sum; </code>
I set the function to be array_sum; added empty parentheses and echo'd the variable %sum.
Please help me.
Btw: related question: why do I have to add empty () parentheses after the function name?
Ernst van Megen
3,541 PointsAlright, one more thing...
$sum = $sum . $number;
returns: 0123
$sum = $number . $sum;
returns: 3210
$number contains 1,2,3,4 $sum = 0
Why is the 3210 one 3210 and not 1230?
4 Answers
Crisoforo Gaspar Hernández
18,302 PointsYes the arguments are for example are like the ingredients for a recipe which is in this case is the function.
Exactly variables only live in a certain scope or sometimes called a block, a block is identified by the curly braces { } so the variable $sum outside the function is totally different from the variable inside the function. Exactly the keyword return inside the function means that the function is going to return a value in this case return the value of the variable $sum, this values is returned and after this is going to saved into the new variable $sum or could be another name for example:
```$result = sum_array($numbers); echo $result
and $result stores the returned value from the function.
Yes and all your comments are correct. When I set the var $sum to zero is just for ensure that the count starts at zero and have a correct sum of the numbers.
Ernst van Megen
3,541 PointsHere is my thought process. Can anyone check what's wrong? I have added Q(uestions) inside the code. Thank you very much.
<?php
// Set a variable $numbers with the array
$numbers = array(1,2,3,4);
// Count the total number of numbers and put it in the variable $total
$total = count($numbers);
// Set the variable sum to zero. Q: Why do we need to se this to zero? Is this because it needs to be a numeric?
$sum = 0;
// Set the output variable to empty. Q: Why does it need to be empty and not 0? Or 4?
$output = "";
// Variable i to 0. Why?
$i = 0;
// Every individual array item is put in an $number variable
foreach($numbers as $number) {
// Variable $i counting, adding one for every $number variable added
$i = $i + 1;
// If the $i variable is less than the total (4) add it to the $number variable. When the variable $i hits 4 it stops.
if ($i < $total) {
$sum = $sum + $number;
}
}
// Echo's the $sum variable that contains the result of the $numbers total
echo $sum;
?>
Ernst van Megen
3,541 PointsOT: Randy Hoyt the Edit function of my first post/question does not work in chrome. Only works on comments.
Crisoforo Gaspar Hernández
18,302 PointsThe parentheses after a function name is for specify the argument list, when is empty it means that the function does need a special list of values to work with.
For example
```function sum($a, $b){ return $a + $b; };
It means that the function needs two values, in other case if the parentheses are empty we don't need special values (arguments).
```<?php
function sum_array($array = []){
// Return 0 if the argument is not an array or if the
// array is empty
if(!is_array($array) || count($array) == 0) return 0;
$sum = 0;
foreach($array as $number){
// Short notation for sum = sum + number;
// and ensure that $number is always a number with (int)
$sum += (int)$number;
}
return $sum;
}
// usage
$numbers = array(1,2,3,4,5,6,7,8,9,10);
// call to the function and save the value to the var $sum
// where $sum is different from the variable $sum of the inside of the function
$sum = sum_array($numbers);
// $sum should be 55
echo $sum;
Hope that work for you! ;)
Ernst van Megen
3,541 PointsThank you Crisoforo Gaspar Hernández or explaining the empty parenthesis question! If I understand it correctly the arguments are a 'pre-check' for the content of the function. What should be done in what certain cases.
In the code you first create a function with the $sum variable which you can only use inside that function. After that you have to set $sum again but this time with the function name sum_array. Correct?
Are all my comments correct in my 2nd post?
Ernst van Megen
3,541 PointsErnst van Megen
3,541 Points