PHP Functions

There are two types of functions in PHP. The built-in function and the User defined (custom) function.

a. Built-in functions: PHP has thousands of built-in functions some of which we have been using since, for example the echo or print, date, array, mail, math, msqli, strings etc.

Date funtion example

<?php
print date('h:i:s - d/m/Y');
?>

Output

Current time and date
09:28:09 - 27/06/2022

pi() math funtion example

<?php
print pi();
?>

Output

3.14159265359

b. Custom function: or user-define function as the name suggest, user-define function are function you create to solve a particular problem or execute a task that occurs severally, this save you from the time or effort required to rewrite such code. So you create such function once and call it whenever you need.

Example

<?php
//create a funtion
function WelcomeMsg(){
  echo "Welcome to my site";
}
//call the function
WelcomeMsg();
?>

Output

Welcome to my site

Example returning values

<?php
//create a funtion with parameters
function AddNum($x, $y){
  return $x+$y;
}
//call the function
echo AddNum(200,700);
?>

Output

900




Next »


Home    |    Learn Programming   |    Learning Management System   |    Computer Based Testing System    |    Contact Us