What is a function ?
A function is a portion of code which performs a specific task and it independent from the rest of the code.
PHP Code:
function add($numberOne, $numberTwo) {
$sum = $numberOne + $numberTwo;
return $sum;
}
This function takes two parameters, $numberOne and $numberTwo. Then assigns $sum to the sum of the two variables. After that we return the variable $sum.
Lets explain each part !
PHP Code:
function add($numberOne, $numberTwo)
We start a function by using the keyword function. Every function has a name (unless it's a closure, anonymous function, etc, but that's another subject). So the name of our function is add. In our function add we are passing two parameters $numberOne and $numberTwo. This allows us to pass values to our function, you are able to have functions that get passed no values, it would look like this
PHP Code:
function add() { }
Next we add the two parameters in and assign it to the variable $sum. This is pretty self explanatory.
This returns the value we assigned to $sum. In the case of this function we could take this value and print it to the screen.
PHP Code:
$add = add(1,1); // Return value is 2
echo $add; // Print 2 to the screen
Well that's it for this tutorial ! Hope it helps you out !