Mattle Posted July 12, 2011 Posted July 12, 2011 (edited) In the previous part we've learned that PHP uses a if ... else structure and the use of variables. In this part I will learn you some more about functions. What is a function? A function is a piece of code which can be reused with one simple line of code. Everytime when you call the function the exact piece of code will be executed. The advantage is that you only have to write the code one time. There are three types of functions: 1. Built-in PHP functions (examples are echo, print, empty) 2. Own build functions 3. Extern libaries (For example: Handling queries in MySQL, we will talk about those also in another part) You have to know some things about functions: 1. A function always has a name. 2. The function's name is case-sensitive, so 'SampleFunction' isn't the same functions as 'samplefunction' 3. A function's name always has to begin with a alpacharacter. 4. A function can use parameters, which it can uses inside it's code. 5. You call a function by writing it's name and after it some ()'s, inside those () you can put the data for the parameters. For example SampleFunction('mattle'); Built-in PHP functions PHP knows a lot of built-in functions which can be used in your code to convert, check, get, send and process your data or code. As there are simple too many to explain I will try to explain the most used ones. You can check all the functions at http://nl.php.net/manual/en/funcref.php empty('thestringhere'); This one will check if the given string is empty or not by giving a true back if it's empty and a false if it isn't empty. strlen('thestringhere'); This function will send back the amount of characters of a string. In this case 13. is_numeric(123); This function will check is the given string only contains out of numbers. If not, it will give back a false, if yes a true. So for example if I want to check if a given variable is empty we use this: <?php $name = 'Mattle'; if(!empty($name)) { echo 'Name is filled in.'; } else { echo 'No name is given.'; } ?>This script will print the text 'Name is filled in.' Own made functions An own made function can be used for several things, I will make a simple functions which adds 2 numbers and then return it back to where the functions is called. An important thing is that you first have to create the function before you can call it somewhere. You create a function by first letting PHP know that he is dealing with a custommade function. You do that by this: function FunctionName() { } Between the () you can add as much params as you want. We only need two for the two numbers. We call our function Add2Numbers. This is what we have now: <?php function Add2Numbers($number1, $number2) { } ?>As you see the params are just used like variables. Inside our function we will make the sum and set it into a variable. <?php function Add2Numbers($number1, $number2) { $SumTotal = $number1 + $number2; } ?>As you can see the $SumTotal will keep the whole sum of both numbers. Now we only want to send it back to where it's called. We simply use return for that: <?php function Add2Numbers($number1, $number2) { $SumTotal = $number1 + $number2; return $SumTotal; } ?>As you can see, our functions is ready. Now we want to test is by simply calculate 1 + 4. We will simple call the function and immeadiatly print it's results in the browser. We do it like this: <?php function Add2Numbers($number1, $number2) { $SumTotal = $number1 + $number2; return $SumTotal; } echo Add2Numbers(1, 4); ?>If you did it right, the number 5 should now be shown in your browser. Congratz, you made your first function! I hope you enjoyed this part again. If you have comments, questions just send me and I will help you. Good luck! Edited March 30, 2018 by Fearless Staff Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.