In PHP as in any other programming language, programmers often use something called
functions or methods to keep our code pretty organized and reuse code when needed.
So basically we can say that a function behaves like a little
program, that we can use to reuse code and write less in our programs,
for instance, imagine a calculator program, in which the user can make a
lot of additions, this would require to use code for making this
additions as many times as the user needs, and with a simple function we
can reuse the same code. Lets see how we can create these functions.
How to create a function in PHP
To create functions we most declare them, type in a PHP file the following code to see how it’s done.
07 | echo "Hello eveyone!!" ; |
This was an easy way to create a function and use it right?
In the next video I will explain a little bit more about functions in a more visual way.
PHP function’s Video tutorial
How to use parameters in a PHP Function
Now parameters are a better way to use functions, you define
parameters inside parenthesis in functions, see the example below in
which we can use a function to get the result of the addition of two
diferent numbers.
04 | function add_Numbers( $number_a , $number_b ) |
06 | $result = $number_a + $number_b ; |
That would be all for now, I hope you guys liked this tutorial, please don’t forget to comment below!