This tutorial will teach you how to create your own functions that can contain arrays, other functions, loops, if then and else statements and all of the above combined!
What I described in the introduction really isn't that difficult. As long as you know how to use all of the different loops and built in functions it's quite easy to create your own function containing each and every one of these. To start with lets create a very simple function:
function helloWorld(){ echo("Hello World!"); } helloWorld();
Let's run over this part by part. The function part at the beginning says that the writing afterwards is going to be the name of our new function(in this case helloWorld). This new function can now be called anywhere we want. Here we called it directly after we defined the function. But what about the stuff inside the curly brackets? That's where you tell the function what to do. Here we are saying that every time we call the function it should echo "Hello World!". Functions like this can do everything from echoing things to simple math.
What if you declared a variable earlier in your code and you want to use it in your function. PHP makes this quite easy, allowing you to write the variable right into your function as a parameter. In the first example, if you noticed how after I named and called the function I added () and you wondered why those were there it's because thats where you write your parameters!
function greet($greeting){ echo($greeting); } greet("Hello World!");
First We called the parameter in our function as greet($greeting), where $greeting is the parameter. I then told the Function to echo whatever I put in for $greeting when I called the function. Then when I told the function to greet I put "Hello World" as the parameter $greeting so that it would print "Hello World" to the screen. You can also use a predefined variable such as this:
$greet = "Hello World!"; function greeting($greet) { echo $greet; } greeting($greet);
This gives the exact same thing as the example above except were declaring that $greet is equal to the string "Hello World" before we call it as a parameter. This can be helpful if we intend to call the parameter again and have the same outcome.
If we only want our function to return a value so that we can work with it later instead of printing the result to the screen we can use return.
function doMath(){ return 2 + 2; } doMath();
Return works in exactly the same way as echo except instead of printing the result of the function onto the screen it stores it for later use. In this case the function doMath() returns 4 and although we can't see it now if we typed echo doMath(); at the bottom 4 would print onto the screen.
I could have had two parameters in the function above and it would have looked something like this:
function greet($first, $second){ echo ($first . $second); } greet("Hello ", "World!");
This Function operates in exactly the same way as the first function except I used two parameters instead of just one, and stuck them together using concatenation. You can also have multiple predefined variables in the exact same way that we did above.
Much in the same way that we could use predefined variables as parameters we can also use arrays and specific items in the array:
$array = array("Burger", "Fries", "Onions"); function food($array){ echo " I want a " . $array[0] . " with a side of " . $array[1] . " and " . $array[2] . " On top."; } food($array);
Parameters make writing out an order much easier. Now if I want to make my order all I have to do is write food($array); and PHP does the rest instead of me having to write out the whole thing every time I want to eat.
Creating your own functions can be extremely useful and save you a lot of time writing code. Combining your functions with if, then, else statements and for loops can create extremely powerful PHP programs. Go on to the Conditionals and Loops sections to learn how to utilize these tools and then try and combine them with what you learned here in the User Created Functions section.