In this tutorial, you will learn-

Why use Functions? PHP Built in Functions String Functions Numeric Functions Date Function Why use User Defined Functions?

Why use Functions?

	Better code organization – PHP functions allow us to group blocks of related code that perform a specific task together.

	Reusability – once defined, a function can be called by a number of scripts in our PHP files. This saves us time of reinventing the wheel when we want to perform some routine tasks such as connecting to the database

	Easy maintenance- updates to the system only need to be made in one place.

PHP Built in Functions

Built in functions are predefined functions in PHP that exist in the installation package. These PHP inbuilt functions are what make PHP a very efficient and productive scripting language. The built in functions of PHP can be classified into many categories. Below is the list of the categories.

String Functions

These are functions that manipulate string data, refer to the article on strings for implementation examples of string functions

Numeric Functions

Numeric functions in PHP are the functions that return numeric results. Numeric php function can be used to format numbers, return constants, perform mathematical computations etc. The table below shows the common PHP numeric functions

Date Function The date function is used to format Unix date and time to human readable format. Check the article on PHP date functions for more details. Other functions These include;

	Arrays – see the article on arrays for examples

	Files – see the article on files for examples

Database functions – see the article on MySQL PHP and other database access methods v2

Why use User Defined Functions? User defined functions come in handy when;

	you have routine tasks in your application such as adding data to the database

	performing validation checks on the data

	Authenticating users in the system etc.

These activities will be spread across a number of pages. Creating a function that all these pages can be calling is one of the features that make PHP a powerful scripting language. Before we create our first user defined function, let’s look at the rules that we must follow when creating our own functions.

	Function names must start with a letter or an underscore but not a number

	The function name must be unique

	The function name must not contain spaces

	It is considered a good practice to use descriptive function names.

	Functions can optionally accept parameters and return values too.

Let’s now create our first function. We will create a very basic function that illustrates the major components of a function in PHP. Output: HERE,

	“function…(){…}”  is the function block that tells PHP that you are defining a custom function

	“add_numbers” is the function name that will be called when using the function.

	“()” can be used to pass parameters to the function.

	“echo ‘Hello function!’;” is the function block of code that is executed. It could be any code other than the one used in the above example.

Let’s now look at a fairly complex example that accepts a parameter and display a message just like the above function. Suppose we want to write a function that prints the user name on the screen, we can write a custom function that accepts the user name and displays it on the screen. The code below shows the implementation. Output: HERE,

	“…($name){…” is the function parameter called name and is initialized to nameless. If no parameter is passed to the function, nameless will be displayed as the name. This comes in handy if not supplying any parameter to the function can result in unexpected errors.

Let’s now look at a function that accepts a parameter and then returns a value. We will create a function that converts kilometers to miles. The kilometers will be passed as a parameter. The function will return the miles equivalent to the passed kilometers. The code below shows the implementation. Output:

Summary



	Define function in PHP: Function is a block of code that performs specific task.

	Built in function in PHP is a function that is shipped with PHP

	PHP has over 700 built in functions

	String functions manipulate string data

	Numeric functions manipulate numeric data

	Date functions manipulate date data

	Other functions such as is_array, fopen etc. are used to manipulate arrays and files respectively

	User defined functions are functions that you can create yourself to enhance PHP