How To Write A PHP Function

How To Write A PHP Function
Although the PHP language has many built-in functions, there will be times when you will want to write a customized function for your program. This is easily done with PHP. For example you may want to put a personalized welcome message on every page of your website. To do this you would write a simple function that prints "Welcome, someone's name" at the top of each webpage. But you need to print a different name in this message for each visitor to your site. This is also easy to do with a PHP function. You can pass the visitor's name to the function as a stringWhat? of characters. You will pass this string as an argument of the function call.

Hello_Visitor("Nancy");

If you have not read the tutorial about the PHP Function And Function Call; you may wish to do so now. If you are ready to continue let's take a look at the code that will define or declare the Hello_Visitor function.

Basic PHP Code
function function_name($argument_1, $argument_2)
{ body of function }

Example
function Hello_Visitor($current_name)
{ echo "Welcome, $current_name"; }


function
This tells PHP that the following is a function.

Hello_Visitor
function_name
This is the name that you give to your function. Your program will use this name to identify the function. So you will use the same name in the function call and in the function itself. When naming your functions remember that function names are not case sensitive. So the function name Hello_Visitor is the same as hello_visitor. However, a function name cannot contain any spaces and must begin with a letter or an underscore.

Hello_Visitor($current_name)
Parentheses
The parentheses are used to pass arguments or information to the function. If you do not need to pass any information then you will put nothing between the ( ). But you must still have the parentheses.

($current_name)
($argument_1, $argument_2)
The argument is the variable name that holds the information that you pass to the function. If you have more than one argument, separate them with a comma. Then place the argument(s) between the parentheses. In our example, the string Nancy will be passed to the function and stored in the $current_name argument which is placed between the parentheses.

{ echo "Welcome, $current_name"; }
{}
The code that will make up the body of the function is placed between the opening { curly bracket and closing } curly bracket. In our example the body of the function is an echo statement that will print the welcome message to the browser. Because Nancy is the string stored in the $current_name variable the welcome message will be Welcome, Nancy.

-------------
What is a string: A string is a group of characters such as Nancy. A string must always be placed between single or double quotation marks.
Example --> "Nancy"






This site needs an editor - click to learn more!



RSS
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





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.