PHP Functions

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.

PHP provides various built-in functions to the user. User can also create user-defined functions too.

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

    
 
    function functionName(arg1, arg2, ....) {
	//block of code to be executed
   }

   

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.
  • The PHP function name is case-insensitive (Both totalCost() and TotalCost() refers to the same function.)
  • Always name the 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:

    
  
     functionName();  // calling function
 
   

Source Code

          <?php
 function greetMsg() {
     echo "I have understood php function.";
  }
 greetMsg(); // call the function
?>
        
Try it now

PHP function containing single argument:

Source Code

          <?php
function fruits($name) {
echo 'I like to eat ' . $name ; 
echo '<br>'; // line break
} 
fruits('mango'); 
fruits('orange');
fruits('banana');
?>
        
Try it now

PHP function containing multiple arguments:

Source Code

          <?php
function fruits($name, $city, $cost) {
echo "$name is selling inside $city at the rate of $cost";
}
fruits('mango', 'salempur', 200);
fruits('orange', 'salempur', 50);
?>
        
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

          <?php
function fruits($name) {
$name .= ' is one of the best fruit';
echo   $name . '<br>'; 
// outputs "mango is one of the best fruit"
}
$fruitName = 'mango';
fruits($fruitName);
echo 'Outside the function: ' . $fruitName; // it is stil 'mango'
?>
        
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

          <?php
function fruits(&$name) {
$name .= ' is one of the best fruit';
echo   $name . '<br>'; // outputs "mango is one of the best fruit"
}
$fruitName = 'mango';
fruits($fruitName);
echo 'Outside the function: ' . $fruitName; // it is stil 'mango'
?>
        
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

          <?php
function fruits($=fruitName = 'mango') {
echo "The number is: $number <br>";
}
fruits('orange');
fruits('grapes');
fruits(); 
// will print ,mango, the default value
?>
        
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

          <?php
function addNumber($x, $y) {
$sum = $x + $y;
return $sum;
}
$returnedValue = addNumber(20, 70);
 echo "Function returned the summation of two number:
    $returnedValue";
?>
        
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

          <?php
function fruit() {
echo "Mango is one of the best fruit";
}
$functionName = 'fruit';
$functionName(); // called fruit function
?>
        
Try it now

Web Tutorials

PHP Functions
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4