PHP Functions

PHP function 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.

 

PHP User Defined Functions

Besides the built-in PHP functions, it is possible to create your own functions.

  • A function is a block of statements that can be used repeatedly in a program.
  • A function will not execute automatically when a page loads.
  • A function will be executed by a call to the function.
  • A function name must start with a letter or an underscore. Function names are NOT case-sensitive.

Syntax:

function myFunction()
{  
   //code to be executed  
}  

 

Example:

<?php  
function myFunction()
{  
   echo "Welcome To Elevenstech";  
}  

myFunction();//calling function  
?>

Output:

Welcome To Elevenstech

 

PHP Function Arguments

We can pass the information in PHP function through arguments which is separated by comma.

Example:

<?php  
 function sayHello($name)
 {  
   echo "Hello $name<br/>";  
 }  
 sayHello("Kailash");  
 sayHello("Ranjeet");  
 sayHello("Hemant");  
?>  

Output:

Hello Kailash
Hello Ranjeet
Hello Hemant

 

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 defFunc($str, $num=12)
{
    echo "$str is $num years old \n";
}
 
// Calling the function
defFunc("Amit", 15);
 
// In this call, the default value 12
// will be considered
defFunc("Kailash");
 
?>

Output:

Amit is 15 years old 
Kailash is 12 years old

 

Returning Values from Functions

A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.

You can return more than one value from a function using return array(1,2,3,4).

Example:

<?php
 
// function along with three parameters
function retFunc($num1, $num2, $num3)
{
    $product = $num1 * $num2 * $num3;
     
    return $product; //returning the product
}
 
// storing the returned value
$retValue = retFunc(3, 5, 2);
echo "The return value is $retValue";
 
?>

Output:

The return value is 30

 

Parameter passing to Functions

PHP allows us two ways in which an argument can be passed into a function: 

  • Pass by Value: On passing arguments using pass by value, the value of the argument gets changed within a function, but the original value outside the function remains unchanged. That means a duplicate of the original value is passed as an argument.
  • Pass by Reference: On passing arguments as pass by reference, the original value is passed. Therefore, the original value gets altered. In pass by reference we actually pass the address of the value, where it is stored using ampersand sign(&).
<?php
 
// pass by value
function valFunc($num) {
    $num += 2;
    return $num;
}
 
// pass by reference
function refFunc(&$num) {
    $num += 10;
    return $num;
}
 
$n = 10;
 
valFunc($n);
echo "The original value is still $n \n";
 
refFunc($n);
echo "The original value changes to $n";
 
?>

Output:

The original value is still 10 
The original value changes to 20