PHP Functions Tutorial

PHP Functions

PHP is provided various built-in functions to the user. You can also create user-defined functions too.

What is PHP function ?

A PHP function is a block of statements that can be reused many times. It can take input as an argument list and return value.

PHP function will be only executed by calling the function.

There are two types of function.

  • 1.0 PHP built in function
  • 2.0 PHP user defined function

PHP Built-In Functions

PHP provides various built-in functions. Every PHP built-in function performs some specific task. Let us see it with the help of an example.

  • echo() - to output a string
  • define() - to define a constant
  • var_dump() - to dump data of a variable

PHP User-Defined Functions

PHP user-defined function starts with the function keyword followed by function name and arguments.

General Syntax Of User Defined Functions

User Defined Function Naming Conventions

  • PHP function name will start with a letter or underscore but not a number.
  • Letters, numbers, and underscores will be used after the first letter in a function.
  • PHP function name is case-insensitive (Both totalCost() and TotalCost() refers to the same function.)
  • Always name function as per the function actions.

Calling Function

A function can not run until it is called.Please keep in mind that a function is called by its name followed by () .

General Syntax:

A user-defined function can call as follows:

Source Code

Try it now

PHP function containing single argument:

Source Code

Try it now

PHP function containing multiple arguments:

Source Code

Try it now

PHP Function Arguments - Passing By Reference

There are two ways to pass arguments to a function. An argument can be passed to a function using call by value and by reference. By default function argument is passed by value.

Why Passing By Reference?

Passing argument to a function by reference, allows us to modify function arguments. That is the main reason behind the passing argument by reference.

Passing an argument by reference can be done by prepending ampersand (&) to the argument name in the function definition.

Let us understand the above mention concept using an example.

Source Code

Try it now

Code Explanation:

  • Set the value of $fruitName to "mango".Then it is passed into fruits() function as the $fruitName as a argument.
  • Inside the fruits() function, "mango" is changed to "mango is one of the best fruit".
  • But the global variable $fruitName still holds the value "mango".

Therefore, it is clear that a variable value can be changed inside the function when it passes inside the function by reference. Just prepend an & sign to the argument name in the function definition.

Source Code

Try it now

PHP Function Arguments Having Default Values

The default value for a function's argument can be provided by using assignment operator (=) in the function definition. If the calling function, does not provide the value then the function will be used as the default value of the argument.

Let us understand with the help of an example.

Source Code

Try it now

PHP Function: Returning Value

A function return statement is used to return the value to the calling function. Whenever PHP parser parses the return statement inside the function then it returns the function's value to the calling function and stops the execution of the function.

Let us take an example

Source Code

Try it now

PHP Variable Functions

It is possible to assign function names as strings to variables and then treat these variables exactly as the function name itself.

Source Code

Try it now