You are on page 1of 7

Function

ETL LABS PVT LTD – PHP 83


What is a Function?

• A function is a reusable piece or


block of code that performs a
specific action.
Syntax: • Functions can either return values
when called or can simply perform
an operation without returning any
function functionName() value.
{ • PHP has over 700 functions built in
code to be executed; that perform different tasks.
} • A function name can start with a
letter or underscore (not a number).

ETL LABS PVT LTD – PHP 84


Advantages of functions

• Functions reduces the repetition of code within a program — Function allows you to
extract commonly used block of code into a single component. Now you can perform the
same task by calling this function wherever you want within your script without having to copy
and paste the same block of code again and again.
• Functions makes the code much easier to maintain — Since a function created once can
be used many times, so any changes made inside a function automatically implemented at
all the places without touching the several files.
• Functions makes it easier to eliminate the errors — When the program is subdivided into
functions, if any error occur you know exactly what function causing the error and where to
find it. Therefore, fixing errors becomes much easier.
• Functions can be reused in other application — Because a function is separated from the
rest of the script, it's easy to reuse the same function in other applications just by including
the php file containing those functions.

ETL LABS PVT LTD – PHP 85


Function types

Built-in Functions User-Defined Functions

• Built in functions are functions that • A function is a block of statements that


exist in PHP installation package. can be used repeatedly in a program.
• These built in functions are what • A function will not execute immediately
make PHP a very efficient and when a page loads.
productive scripting language. • A function will be executed by a call to
• The built in functions can be classified the function.
into many categories. Below is the list
of the categories.

ETL LABS PVT LTD – PHP 86


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. 4

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

ETL LABS PVT LTD – PHP 87


Default Argument Value
The example shows how to use a
5 default parameter. If we call the
function setHeight() without
arguments it takes the default value as
argument

ETL LABS PVT LTD – PHP 88


Functions - Returning
values
To let a function return a value, use the
return statement 6

ETL LABS PVT LTD – PHP 89

You might also like