Functions in PHP

A function is a block of code that can be used many times in a program.

It helps us to save a lot of codes and time because function defines only one time and we can use it multiple time whenever & wherever we want.

The most important reason for using function is ‘Reusability’.

Consider an example:
In your program you need a square value of any number more than one time, then what will you do?

You re-type the logic of square value of different number every time.


But using the function you need to declare logic of square of any number only one time and use it anywhere any time without re-typing same code for your program.

PHP User Defined Functions

There are thousands of built-in functions in PHP.

We can also create our own functions in PHP.

PHP function separates the programming logic & easy to understand the flow of code.

How to Define a Function in PHP

A function is a block of statement defined by the developer that can be used repeatedly in a program.

During execution of a program, a function does not execute automatically, the function can be executed upon function call.


The syntax of a function in PHP is as below

NoteFunction name always starts with letter or _ (underscore), never start with integer or any other special characters.

If the function name is too long separate two words by using _ but don’t use space between two words.

PHP is a case-sensitive language so function names are also case sensitive.

Most of the developer follows camel case rules to define function name. Like functionName() not functionname()

Example

Output

Explanation

By typing ‘showMsg()’code you can easily display ‘Hello World’ message anywhere in your program without typing ‘Hello World’ repeatedly.

Passing Function Arguments or Parameters in PHP

The function can take user input as an argument or parameter and return value. The function always returns type.

Function with Arguments or Parameters

Output

Explanation

$num is an argument or parameter which received the number value like 43, 65, 112, or 225.

We can use more than one arguments or parameters by using comma separator.

Using squareOfAnynumbers () you can easily calculate square value of any number in your program.

Example

Function with multiple Arguments or Parameters

Output

Call by value in PHP

PHP allows you to call the function by value. Let’s understand the concept of “call by value” with examples.

Function Call By Value:

Output

Explanation
Call by value means argument ’s value directly passed to the function and the value only modified into the function, not outside of the function.

So, $a only changed into doSomeThing() while passed inside the function, that’s why doSomeThing($a) returns 4 but $a is still 3.

Swap two numbers using Call By Value:

Output

Explanation
$a and $b changed within function so when we call the function swapTwoNumbers($a,$b).

It returns 8 6 means $a = 8 and $b = 6
But outside of the function $a still 6 and $b still 8.

Call By Reference in PHP function

PHP allows function call by reference also. Let’s start to understand with the simple example.

Function Call By Reference:

Output

Explanation

Call by reference means value and address (memory location) both passed to function and value will be modified within the function and also outside of the function.

Here we used & (ampersand) as the reference of argument’s address in the function definition.

So any modification of variable in the function also reflects outside of the function in the original variable.

So, $a return 4 and doSomeThing($a) also return 4.

Swap two numbers using Call By Reference:

Output

Explanation
$a and $b changed within function so when we call the function swapTwoNumbers($a,$b) it returns 8 6
means $a = 8 and $b = 6
and outside of the function $a 8 and $b 6.

Recursion in PHP

In programming, language Recursion occurs when function continues to call itself using some sort of condition.

If there is no condition defined, function calls occur infinite times. These sort of conditions are called the base case.

If a base case is reached function returns the value and stop the recursion.

Until the base case requirement fulfills function automatically calls itself to continue the recursion.

The syntax of the recursive function:

Factorial using the recursive function:

Output

Explanation
Frist base case is $num is less than 0 means $num value is -1 or any negative number then recursion will be stopped and return -1.


The second base case is $num is equal to 0. If $num value is 0 then recursion will be stopped and return 1.


Otherwise fact() automatically called by itself and return the factorial of any positive number.


Here we passed 6 as input, so the base case has not reached then recursion continued for 5th step and returned 720.