PHP Functions

A Function is a reusable piece of code. You will write it once and you can use it many times. For example, We can make a function to perform addition or subtraction or calculation of VAT, And we can use it as many times as we want. Functions have a body structure{} In PHP the keywords for functions are: function, return

To use a function we have two steps:

  • function declaration
  • function invocation (or call)

To declare a function:

you use the keyword function followed by the name of the function, two parentesis and the block of code in between two curly braces:

function fooName(parameters)
// Function body goes here
return $returnValue;

Functions do not have to return anything, so the return statement can be left out.

Variables used within the function are local to that function and do not effect the global scope.

User defined PHP Functions

zero parameter funtion

Using return keyword is optional, here two samples:

returning a value..

without returning a value..

function with parameters

You can parse parameters (also called arguments) to a function to make it work with them.


Here other samples of user defined functions:

  • add()

  • subtract()

  • vat()

PHP Built-in Functions

PHP comes with many built-in (or internal) functions. We don’t have to define those functions. We just need to use them, by calling their names and passing them values. A list of all PHP builtin functions is given at php manual funtion reference

PHP has nearly 3,000 built in functions covering pretty much anything you could want to do with a web application. Luckily, it also has a fantastic manual. The manual not only includes full documentation and example code for almost every function but also provides a user comments feature which is full of valuable tips. If you are having a problem with a PHP function check the manual page – the chances are someone else will have had the same problem and documented a solution in the comments section.

A useful shortcut for looking up the manual page for a function is to type straight in to your browqser – the PHP site will redirect you to the manual page for that function. For example, takes you straight to the manual page for the date() function.

Working with date() function:

Functions to check a variable:


Using form values:


Using print_r():


Using mail() to send an email:

All built-in PHP functions can be found at:

Click here to read about how to send an email.