1. php
  2. /basics
  3. /functions

Introduction to PHP functions


In PHP, a function is a block of code that can be reused multiple times throughout a script. Functions are defined using the "function" keyword, and can take one or more parameters (also known as arguments) as input. There are several types of functions in PHP including Built-in functions, User-defined functions

Built-in functions

Some common PHP Built-in functions include:

  1. array_merge() - This function merges one or more arrays together and returns a new array.
  2. count() - This function returns the number of elements in an array.
  3. in_array() - This function checks if a specific value exists in an array and returns a boolean value.
  4. sort() - This function sorts the elements in an array in ascending order.
  5. array_slice() - This function returns a portion of an array, specified by a start and end position.
  6. array_keys() - This function returns an array of all the keys in an associative array.
  7. array_values() - This function returns an array of all the values in an associative array.
  8. array_diff() - This function compares two or more arrays and returns an array of values that are present in the first array but not in the other arrays.
  9. array_intersect() - This function compares two or more arrays and returns an array of values that are present in all of the arrays.
  10. array_search() - This function searches for a specific value in an array and returns the key if found, otherwise returns false.

Examples of built-in functions

  1. array_merge()
$array1 = array("a" => "red", "b" => "green");
$array2 = array("c" => "blue", "d" => "yellow");
$result = array_merge($array1, $array2);


Array ( [a] => red [b] => green [c] => blue [d] => yellow )
  1. count()
$fruits = array("apple", "banana", "orange", "mango");
echo count($fruits);


  1. in_array()
$fruits = array("apple", "banana", "orange", "mango");
if(in_array("banana", $fruits)) {
    echo "Banana is in the array.";
} else {
    echo "Banana is not in the array.";


Banana is in the array.
  1. sort()
$numbers = array(4, 2, 8, 3, 1, 5);


Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 8 )
  1. array_slice()
$fruits = array("apple", "banana", "orange", "mango", "kiwi");
$slice = array_slice($fruits, 1, 3);


Array ( [0] => banana [1] => orange [2] => mango )
  1. array_keys()
$fruits = array("a" => "apple", "b" => "banana", "c" => "orange");
$keys = array_keys($fruits);


Array ( [0] => a [1] => b [2] => c )
  1. array_values()
$fruits = array("a" => "apple", "b" => "banana", "c" => "orange");
$values = array_values($fruits);


Array ( [0] => apple [1] => banana [2] => orange )
  1. array_diff()
$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "b" => "black", "d" => "pink");
$result = array_diff($array1, $array2);


Array ( [c] => blue )
  1. array_intersect()
$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("b" => "green", "c" => "blue", "d" => "pink");
$result = array_intersect($array1, $array2);


Array ( [b] => green [c] => blue )
  1. array_search()
$fruits = array("a" => "apple", "b" => "banana", "c" => "orange", "d" => "mango");
$key = array_search("banana", $fruits);
if ($key !== false) {
    echo "The key of the banana is: $key";
} else {
    echo "Banana is not in the array";


The key of orange is: 2

These are just some examples of the many built-in functions that PHP provides, there are many more functions which can be used for various purposes such as handling files, HTTP, session, image processing, cryptography and other.

User Defined Functions

A custom function in PHP is a user-defined function, which means it is created by the user for specific purposes. Custom functions in PHP are created using the "function" keyword, followed by the function name, and a set of parentheses that may or may not contain parameters. The general syntax for creating a custom function in PHP is:

function function_name($parameter1, $parameter2, ...) {
    // code to be executed

For example:

function add_numbers($x, $y) {
    return $x + $y;

Custom functions can also take default value for parameters and return statement can be used to return a value from the function after processing.

function greet_user($name = "Guest") {
    return "Hello, $name";

Custom functions can be called from anywhere in the script by using the function name followed by parentheses containing any necessary arguments.

$result = add_numbers(5, 10);
echo $result; // will output 15

echo greet_user("John"); // will output "Hello, John"
echo greet_user(); // will output "Hello, Guest"

Custom functions are useful because they allow you to modularize your code, making it more organized and easier to maintain. They also allow you to reuse code, reducing the amount of duplicated code in your scripts.

Best Practices

  1. Use clear and descriptive function names that accurately reflect their purpose.
  2. Use proper variable and function scoping to minimize the risk of naming conflicts.
  3. Use appropriate data types for function parameters and return values.
  4. Include clear and comprehensive documentation for each function, including details on its parameters, return values, and any exceptions that may be thrown.
  5. Use appropriate error handling and validation to ensure that the function can handle unexpected input and return appropriate responses.
  6. Use consistent naming conventions and formatting to make the code more readable and maintainable.
  7. Avoid using global variables within functions, as this can make the function's behavior difficult to predict and understand.
  8. Keep functions small and modular, breaking down large functions into smaller, more manageable units.
  9. Use built-in functions and libraries when available, rather than writing your own code to accomplish the same task.
  10. Test functions thoroughly to ensure they are functioning as expected.