logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA
Irish Culture
Home Finance


dailyclick
All times in EST

Low Carb: 8:00 PM

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

How To Write A PHP Function

Guest Author - Diane Cipollo

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!

Add How+To+Write+A+PHP+Function to Twitter Add How+To+Write+A+PHP+Function to Facebook Add How+To+Write+A+PHP+Function to MySpace Add How+To+Write+A+PHP+Function to Del.icio.us Digg How+To+Write+A+PHP+Function Add How+To+Write+A+PHP+Function to Yahoo My Web Add How+To+Write+A+PHP+Function to Google Bookmarks Add How+To+Write+A+PHP+Function to Stumbleupon Add How+To+Write+A+PHP+Function to Reddit




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


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 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.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor