![]() |
| Home | Forums | Articles | Snippets | Experts | Marketplace |
|
|
#1 |
|
Administrator
Join Date: Oct 2008
Posts: 7
|
In php as you write your code it is very likely that you will find yourself doing one action over and over possibly one that is very lengthy and when it’s lengthy it’s not always fun to make the code each time, this is the very reason functions were designed.
A function is a declared piece of code that has code as members of it. All the things you use when you are coding are functions like print(), echo() and exit(). You can create your own functions in PHP to do similar things as these. Let’s look at the layout for writing a function. PHP Code:
Now you are a probably wondering about my little blurb up above about arguments, so let’s explain those. An argument is basically data in the form of variables you desire to pass to a function to have it use in its execution. Another reason you would pass an argument is if you wanted to use a variable declared outside of the function inside, that is variable scope, I will cover that more later on in the tutorial. Basically, when you want to pass a function some arguments you put them inside the parenthesis after the name of the function, which is required even if you don’t have arguments. When declaring an argument to be used in the function it is always in the form of a variable, regardless of whether or not it is going to be called as one, you’ll see what I mean in a minute. Another thing to remember is that when you call a function that has variables declared to it, you must pass the variables in the order they were declared. Also note that when you are declaring or passing variables to an argument, each are separated by a comma. Okay that was a lot of confusing text so let me clear it up with some code for you. PHP Code:
Variable scope refers to where variables are equal to something, when you are using a function $var1 outside the function is not the same as $var1 inside the function, and vise versa. That is why you pass the variable you want used inside the function as an argument, but what if you want to retrieve a variable declared inside the function, outside the function? That is what return() the return function is used to pass a variable (and it’s value) out of the code once it is created or modified within bare in mind that return() is somewhat like exit() in that it halts the execution of the function, so don’t put it in the middle of your function. Let’s make an example: PHP Code:
PHP Code:
PHP Code:
Wow, we did it, that’s pretty much everything you really need to know about functions, the only other thing is recursion which refers to calling a function within itself, but because recursion is almost never used except for very complex system I don’t think it is necessary to include it in this tutorial. In closing, let me provide you with a note about naming functions. When you name a function there are a couple simple things to keep in mind. Your function name cannot be the same as another function’s name. Your function name can only contain letters, digits, and underscores. Your function name cannot start with a digit. The following names are legal: PHP Code:
PHP Code:
So there you go, that’s pretty much everything there is to know about functions, happy programming.
__________________
Help out Coder Central! |
|
|
|