In this article, we will cover the following topics:
- Introduction of Functions
- Declaring Functions
- Passing Arguments to Functions
- PHP Variables Scope
- Local and Global Scope
- Returning Values from a Function
- Recursive Function
- Predefined Functions
Introduction of PHP Functions
PHP functions are a piece of code that can be reused.
Advantages of PHP functions:
- Code Reusability.
- Reducing code duplication.
- Break a larger number of codes into small pieces.
PHP functions are divided into two sections such as Built-in function and user-defined function. Built-in functions are part of the language of PHP. User-Defined function, that can be created by a programmer according to needs. The user-Defined function executes by the call of the function.
Declaring Functions:
A PHP function declares by
keyword
function.
Syntax:
1 2 3 4 5 | <?php function userDefineFunctionName() { //Block of Code } ?> |
In the above syntax,
- Start a keyword
function
for creating a function. - Put the name of function example: userDefineFunctionName.
- Start a curly(
{
) brace. - Write a block of code.
- Close curly(
}
) brace.
Example:
1 2 3 4 5 6 | <?php function add() { echo 10 + 7; } add();//Output: 17 ?> |
In the above example, define a function “add” and it is adding two values (10 + 7). After declaration, it will show 17 as an output. If you do not declare the function then it will not show anything.
Passing Arguments to Functions:
PHP function allows passing arguments to the function. You can pass multiple arguments according to requirements.
1 2 3 4 5 6 | <?php function operations($a, $b) { echo $a + $b; } operations(10,5);//Output: 15 ?> |
Let’s take an example using a switch case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php function operations($a, $b, $operator) { switch($operator) { case '+': echo $a + $b; break; case '-': echo $a - $b; break; case '*': echo $a * $b; break; case '/': echo $a / $b; break; case '%': echo $a % $b; break; default: echo "Enter correct operator."; break; } } operations(12,18,'+');//Output: 30 operations(20,15,'-');//Output: 5 operations(12,3,'*');//Output: 36 operations(15,5,'/');//Output: 3 operations(16,3,'%');//Output: 1 ?> |
PHP Variables Scope:
PHP variable scope is a part of a program that can be accessed or visible.
There are three types of Variable scope:
- Local Scope
- Global Scope
Local and Global Scope:
A variable declares inside the function is called Local Scope that accesses only inside the function.
Example:
1 2 3 4 5 6 7 8 | <?php function checkLocalScope() { $var = 5; //Local Scope echo $var; } checkLocalScope(); echo $var;//It will show error: "PHP Notice: Undefined variable" ?> |
A variable declares outside the function is called Global Scope that can be accessed inside the function by
global
keyword.
1 2 3 4 5 6 7 8 9 | <?php $x = 5; function checkGlobalScope() { global $x; echo $x; } checkGlobalScope(); echo $x; ?> |
PHP Global scope variable also accesses by $GLOBALS array. It associates with the key of the name of the variable.
1 2 3 4 5 6 7 | <?php $x = 5; function globalVariable() { echo $GLOBALS['x']; } globalVariable(); ?> |
Returning Values from a Function
You can return value, array, or object by return keyword in the function and the return stops the execution of the function.
Example:
1 2 3 4 5 6 7 8 9 10 | <?php function add($a, $b) { $c = $a + $b; return $c; } $a= 10; $b = 15; $sum = add($a, $b);//Assign the return value after calling the function to $sum variable. echo "The Sum of $a and $b: ".$sum; ?> |
In the above example, the function is written for adding two numbers and returning the sum of two values($a, $b). Assigned the return value after calling the function to the
$sum
variable and it will show the following output:
Output:
1 | The Sum of 10 and 15: 25 |
Recursive Function in PHP
A recursive function is a function that calls itself continually until the terminating state arrives.
Let’s take an example:
1 2 3 4 5 6 7 8 9 10 11 | <?php function mathTable($var, $i) { if($i <= 10) { echo $var * $i.","; mathTable($var, ++$i); } else { return false; } } mathTable(2, 1); ?> |
In the above example, printing the table of 2 without using the PHP loop.
Output:
1 | 2 4 6 8 10 12 14 16 18 20 |
Predefined Functions in PHP:
PHP has many pre-defined functions which are called built-in functions.
You can check those functions here: https://www.php.net/manual/en/indexes.functions.php
One comment