PHP Functions

The function is a self-contained block of statements that can repeatedly be executed whenever we need it.

The concept of the function is the same as in other languages like C. There are more than 1,000 inbuilt functions into the standard PHP distribution. Apart from these, we can define own functions according to the requirements.

Syntax:
 function functionName(){
    statement 1 :
    statement 2 :
    statement 3 :
    ...... 
 }

Types of Functions in PHP

There are two types of functions as:
  1. Internal (built-in) Functions
  2. User Defined Functions

Internal Built-in Functions in PHP

PHP comes standard with many functions and constructs. i.e.
phpinfo()print()mysqli_connect()error_reporting()
error_log()array()copy()unlink()
date()time()strlen()strlen()

For a complete reference and examples of the predefine functions, please visit http://php.net/manual/en/funcref.php

Some (not even most) usage of built-in functions are as mentioned below:

  • Converting a string of letters to uppercase and lowercase
  • Displaying and using the date and time
  • Initializing and closing a database connection
  • Declaring and using an array
  • Handling files
  • Accessing data in forms
  • Filesystem Functions
  • Function to open FTP connections
  • Email related functions
  • Mathematical Functions
  • MySQL specific functions
  • URL Functions
  • Image functions

Using Your own PHP Function (User Defined Functions)

  • You can also create your own functions.
  • First, you need to give it a name.
  • All functions in PHP start with function().
  • Functions can also accept parameters.
  • A function can also be used to return values.
Example:
<?php 
function myFunction(){
  echo "Example PHP function";
}
myFunction();
?>
Example:
<?php
Function add($a,$b){
  $total = $a + $b;
  return $total; //Function return, No output is shown
}

echo add(55,20); //Outputs 75
?>

Returning Values

All functions in PHP return a value, even if you don't explicitly cause them to. Thus, the concept of "void" functions does not apply to PHP. You can specify the return value of your function by using the return keyword:

Example:
<?php
function hello(){
  return "Hello World"; // No output is shown
}
$txt = hello(); // Assigns the return value "Hello World" to $txt
echo hello(); // Direct Displays "Hello World"
?>

Note, however, that even if you don't return a value, PHP will still cause your function to return NULL.

PHP Function Arguments

Information can be passed to functions through arguments. The following example has a function with two arguments:

Example:
<?php
function addFunction($number, $number)
{
  $number = $number + $number;
  echo "The sum of two numbers is : $number"; //Outputs as 30
}
addFunction(10, 20);
?>

PHP Default Argument Value

The following example shows how to use a default parameter. If we call the function setWeight() without arguments it takes the default value as an argument:

Example:
<?php
function setHeight($minWeight=50){
  echo "The weight is: $minWeight <br>";
}

setWeight(100);
setWeight(125);
setWeight(75);
setWeight();  // will use the default value of 50
?>