The PHP Function and Function Call
|When you are studying PHP you will want to hand code every line in your program so that you can study how the code works. Somewhere along the way you will realize that there are certain tasks that your program needs to perform again and again. And therefore you find yourself writing the same lines of code again and again. For example there are several occasions when a visitor to your website would need to give you an email address. He may want to buy something, sign up for your newsletter or fill in a customer service form. Each time he enters his email address and submits the web form, you will want your program to check the accuracy of the email address. You could hand code this task at each location in your program that you need to check the email address. But that isn't very efficient. Well, PHP has an answer for this problem. It is called a function.|
There are basically two types of PHP functions. The first type is the built-in function. These functions are already part of the PHP language and therefore you do not need to write them yourself. To use these built-in functions you just add a function call to your program. A function call is a piece of code that tells your program to "call in" the built-in function whenever you need it. The second type of function is the user defined function. These are the functions that you write yourself. Then, after you write the function, you can call it into your program any time by coding a function call. Let's take a look at the PHP code for a function call.
Basic PHP Code
This is the name given to the function and is how your program will identify the function. I have used the built-in print function as an example. The function_name for the print function is print and it is placed on the left side of the ( ).
The parenthesis are used to pass information to the function. If you do not need to pass any information then you will put nothing between the ( ).
The argument is the information that you want to pass to the function. If you have more than one argument, separate them with a comma. Then place the argument(s) between the parenthesis. In our example, the argument (information) passed to the function is the $message variable which is placed between the parenthesis. The built-in print function will print whatever value you have given to the $message variable.
The semicolon ends the function call
Note: A function usually passes information back to the program when the function is completed. For example the print function passes a Boolean back to the program.
This site needs an editor - click to learn more!
Editor's Picks Articles
Top Ten Articles
Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.