I thought it was about time to sit down and write part 2 to my tutorial. I am assuming everyone has mastered the concepts in part 1, or at least understands the commands we have gone through. I want to cover just a couple topics in this tutorial, mainly functions and arrays.
Functions
Functions are like mini-scripts. They do the same type of thing over and over as many times as you need. There are many different ways they work and can be implemented in a variety of ways. Lets get started and cover the syntax:
<?php function my_function($variables) { //code here } ?>
So first off, we declare the function by stating it is a function and giving it a name we can reference later. Typically, function names should represent what they do, so if your function adds a bunch of numbers, you might call it “addNumbers”, or if it connects to a database and grabs data, you may call it “connection”. Ultimately the decision is yours.
Secondly, the parenthesis contain the variables that we “pass into” the function, and is optional. I will explain this later as we get a little more in depth with functions.
Third, our code is surrounded by our brackets { }.
So what are we going to put into the function? Since our basic code is echo "Hello World"; let’s go ahead and put that into the function and create a script to “call” our function.
<?php function helloworld() { echo "Hello World!"; echo "<br>"; } //Our main code below echo "This code isn't in the function, but is main code!<br>"; helloworld(); helloworld(); ?>
Output:
This code isn't in the function, but is main code! Hello World! Hello World!
And now the explanation regarding the output. As the script is ran, PHP doesn’t automatically call the function and run the contents, it waits for the main code to call the function into use. In order to call the function, we just type the name of the function followed by the parenthesis. In a sense, by typing the one word, it is exactly the same as typing the the contents of the function. If you had repetitive code, it might be easier to set up a function that will do everything for you very quickly. In the example we just did, it doesn’t make a whole lot of sense to set up a function to do 2 lines of code, but if your script had 50 lines, it might mean the difference between 1000 lines of code and 200.
In the beginning, I discussed passing variables into the function: function my_function($variables). $variables is variable, and you can name it whatever you want. The purpose is so your function can function differently and not just do the same thing over and over. Let’s say you wanted to create a simple script that will count to 10 and multiply each number by 2 as you are counting. That would be impossible if our function didn’t allow us to pass variables to it. Lets set it up:
<?php function multiply($number) { echo $number." - "; $newnumber=$number*2; echo $newnumber; echo "<br>"; } for($a=1;$a<=10;$a++) { multiply($a); } ?>
Output:
1 - 2 2 - 4 3 - 6 4 - 8 5 - 10 6 - 12 7 - 14 8 - 16 9 - 18 10 - 20
You can set up a function to take in 100 variables if you wanted. I could have set up the function to take in all 10 numbers at once: function multiply($num1, $num2, $num3, $num4, $num5, $num6, $num7, $num8, $num9, $num10) and then simply called the function in one line using: multiply(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);. When you use multiple variables they need to separated by a comma every time. You can pass any type of variable you want into a function, it just depends on the code you plan on using with that variable.
Here comes the twist, we can have a function RETURN variables to us, to use later on. There are thousands of scenarios that come to mind on why you would want to return a variable and it is a very common practice. You maybe want to echo the results yourself later on in the script, you may have a function that just checks variables for a specific pattern, you may have a function that takes a string and formats it a certain way, you may have a function that takes data and inserts it into an array for you. Like I said there are thousands of other reasons you would want to have it return a variable. Lets do a quick scenario where we will have one function return a number that is multiplied by 2, and another function tell us if the variable is evenly divisible by 3.
<?php function multiply($num) { $newnum=$num*2; return $newnum; } function isdivisible($num) { if($num%3==0) return true; else return false; } for($a=1;$a<=15;$a++) { $number=multiply($a); $trueorfalse=isdivisible($number); echo $a." - ".$number; if($trueorfalse==true) echo " (Divisible!)"; echo "<br>"; } ?>
Output:
1 - 2 2 - 4 3 - 6 (Divisible!) 4 - 8 5 - 10 6 - 12 (Divisible!) 7 - 14 8 - 16 9 - 18 (Divisible!) 10 - 20 11 - 22 12 - 24 (Divisible!) 13 - 26 14 - 28 15 - 30 (Divisible!)
A quick review for this example. The first function multiplied the number by 2 and then returned the newly created integer. The second function used the modulus operator to check if the newly created number was evenly divisible by 3. If it was it returned the boolean TRUE, otherwise it returned FALSE. We were then able to have it do something different based on what the returned result was. If it was true, we would show that it was divisible by 3, otherwise, we didn’t do anything special.
A quick recap on functions, they can be called almost anything, and can have 0 variables passed into them, up to hundreds if you really needed it. They act like mini-scripts and repeatedly execute the same scripts over and over when called. They can return variables for use later on in any format you want. They simplify every script you create and will perform the menial scripts you hate to type out over and over.
When you look up php.net and examine code…. say the code to make a string of lower case letters into uppercase : strtoupper($string), you will notice that it is a function. PHP has thousands of hardcoded functions that you will use all the time in your coding. All of them simplify the amount of work that you have to do to make your scripts work properly and quickly.
Next up: Arrays (to come later).
I could use your help, please contact me.
Hey,
I know your array section is coming later but it’s a pretty simple. Please review the following:
array( "red" => "apples", "yellow" => "bananas", "orange" => "oranges" ), "veggies" => array( "orange" => "carrots", "brown" => "potatoes", "green" => "peas" ) ); echo $array["fruits"]["orange"] . "" . $array["veggies"]["orange"]; ?>As you can see- arrays can be fun!
Thanks. It is really fun