UNIT-3:FUNCTIONS
GFGC, SHIMOGA Page 1
PHP Functions: It is a piece of code that can be reused many times. It can take input as
argument list and return value. There are thousands of built-in functions in PHP.
In PHP, we can define Conditional function, Function within Function and Recursive
function also.
Advantage of PHP Functions
Code Reusability: PHP functions are defined only once and can be invoked many times, like
in other programming languages.
Less Code: It saves a lot of code because you don't need to write the logic many times. By the
use of function, you can write the logic only once and reuse it.
Easy to understand: PHP functions separate the programming logic. So it is easier to
understand the flow of the application because every logic is divided in the form of functions.
PHP provides us with two major types of functions:
1. Built-in functions : PHP provides us with huge collection of built-in library functions.
These functions are already coded and stored in form of functions. To use those we just
need to call them as per our requirement like, var_dump, fopen(), print_r(), gettype() and
so on.
2. User Defined Functions : Apart from the built-in functions, PHP allows us to create our
own customised functions called the user-defined functions. Using this we can create our
own packages of code and use it wherever necessary by simply calling it.
Creating a Function
• While creating a user defined function we need to keep few things in mind:
• Any name ending with an open and closed parenthesis is a function.
• A function name always begins with the keyword function.
• To call a function we just need to write its name followed by the parenthesis
• A function name cannot start with a number. It can start with an alphabet or
underscore.
• A function name is not case-sensitive.
Syntax: function functionname(){
//code to be executed
}
Example:
function sayHello(){
echo "Hello PHP Function";
}
Call a Function: To call the function, just write its name followed by parentheses
<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
Output: Hello PHP Function
In our example, we create a function named sayHello().
The opening curly brace { indicates the beginning of the function code, and the closing curly
brace } indicates the end of the function.
The function outputs Hello PHP Function
PHP Function Arguments: We can pass the information in PHP function through arguments
which is separated by comma.
PHP supports Call by Value (default), Call by Reference, Default argument
values and Variable-length argument list.
Syntax:
function function_name($first_parameter, $second_parameter) {
//executable code
}
Example
<?php
// function along with three parameters
function fun($num1, $num2, $num3)
{
$product = $num1 * $num2 * $num3;
echo "The product is $product";
}
// Calling the function
// Passing three arguments
fun(2, 3, 5);
?>
Output: The product is 30
Setting Default Values for Function parameter: PHP allows us to set default argument
values for function parameters. If we do not pass any argument for a parameter with default
value then PHP will use the default set value for this parameter in the function call.
Example:
<?php
//function with default parameter
function fun($str, $num=12)
{
echo "$str is $num years old\n”;
}
// Calling the function
fun(“Ram”,15);
// In this call, the default value 12 will be considered
fun(“Adam”);
?>
Output:
Ram is 15 years old
Adam is 12 years old
- Teacher: Admin User