PHP Functions
PHP Built-in Functions:
PHP has over 1000 built-in functions that can be called directly, from within a script, to
perform a specific task.
Please check out our PHP reference for a complete overview of the PHP built-in functions.
PHP User Defined Functions:
Besides the built-in PHP functions, it is possible to create your own functions.
•A function is a block of statements that can be used repeatedly in a program.
•A function will not execute automatically when a page loads.
•A function will be executed by a call to the function.
Create a Function
• A user-defined function declaration starts with the keyword function,
followed by the name of the function:
Note: A function name must start with a letter or an underscore. Function names are
NOT case-sensitive.
Call a Function
• To call the function, just write its name followed by parentheses ():
PHP Function Arguments:
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you
want, just separate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function is called, we
also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first
names, but an equal last name:
PHP Function Arguments:
• The following example has a function with two arguments ($fname and
$year):
PHP Default Argument Value
• The following example shows how to use a default parameter. If we call
the function setHeight() without arguments it takes the default value as
argument:
PHP Functions - Returning values
• To let a function return a value, use the return statement:
Passing Arguments by Reference
• In PHP, arguments are usually passed by value, which means that a
copy of the value is used in the function and the variable that was
passed into the function cannot be changed.
• When a function argument is passed by reference, changes to the
argument also change the variable that was passed in. To turn a
function argument into a reference, the & operator is used:
Passing Arguments by Reference