Tip:
Highlight text to annotate it
X
In php you can create your own custom functions. In this particular function I'm going to add
two numbers display the results. So the first thing I do is create my function. I put in
term function. And I'm going to put in addNumbers. These variables in parenthesis are what's
going to get passed to my function. You can see here I'm going to accept two numbers and
then you'll put your bracket. I'm going to create a variable called sumOfNumbers and
it's going to be equal to the sum of the variable number1 and variable number2 which is getting
passed in to my function. Then I'm going to print and say "The sum of variable number1
and variable number2 is the variable sumOfNumbers. You can see right here i've typed in my function
name. The are the two number's I'm going to have added and printed. And once again you
can see I call the function one more time except this time I used the numbers 5. So
let's see how it works. You can see each time it ran each function twice and put in my two
numbers and added them together.
Second simple example of functions is here I'm going to pass the function two numbers
and perform a calculation and then return a result back. Here you can see I created
my function. I gonna say addDoubleNumbers. What I'm going to do is add two numbers together
and multiply that by two. So I'm accepting my two numbers. Create a variable called doubledNumbers.
And I put the addition of the two number variables inside the parenthesis and then I multiply
it by two. And then I'm going to return the value $doubledNumbers. You can see write here
I typed in my function addDoubledNumbers. These are the two numbers that I passed to
it. The return result right here is going to be returned to this variable called theResult.
And then I'm just going to print "The result of doubling the sum of the numbers is the
variable theResult. So you can see here I added two and three together and doubled it
and it returned 10.