PHP Functions

A function is a block of statements that perform a specific task, which can be used multiple times within a program. In PHP, as with a lot of languages, there are a number of functions built in, however, it is also possible to create user defined functions. The statements within a function are only executed once the function is called, so, if the function is never called then they won't run. In PHP, a function can be defined and called as follows.

// Function definition.
function functionName()
{
   // Statement(s) to execute.
}

// Function call.
functionName();

The function name is not case sensitive, so 'functionName();' and 'functionname();' are both valid calls to the above function. Although numbers can be used within a function name, it must always start with either a letter or an underscore. 

The example below shows a call to a function that outputs the message 'Hello World'.

// Function definition.
function helloWorld()
{
   // Display Hello World message.
   echo "Hello World";
}

// Function call.
helloWorld();

With functions, it is also possible to pass in one or more arguments. This is done by including them within the parentheses, separating them with commas if there is more than one. The following example passes in two arguments, 'fname' and 'sname', for first name and surname, and displays a hello message incorporating these two arguments. Note that a '.' is used to concatenate separate parts of a string together to form the message.

// Function definition.
function helloPerson($fname, $sname)
{
   // Display Hello person message.
   echo "Hello " . $fname . " " . $sname;
}

// Function call.
helloPerson("Fred","Bloggs");

It is possible to specify default values for arguments when a function is defined, so if a function is called without any arguments then these default values can be used. The above example can now be altered so that 'fname' and 'sname' have default values of 'Unspecified' and if there are no arguments in the function call then a hello world message is displayed, otherwise the hello person message is displayed.

// Function definition.
function helloPerson($fname = "Unspecified", $sname = "Unspecified")
{
   // If no arguments have been specified then display a hello world message.
   if ($fname == "Unspecified" and $sname == "Unspecified")
   {
      // Display Hello world message.
      echo "Hello World";
   }
   else
   {
      // Display Hello person message.
      echo "Hello " . $fname . " " . $sname;
   }
}

// Function call.
helloPerson();

As well as being able to pass one or more arguments into a function, it is also possible for a value to be returned. The example below again takes two arguments, first name and surname and returns a hello message string incorporating the two arguments, which is output onto the screen.

// Function definition.
function helloPerson($fname, $sname)
{
   // Display Hello person message.
   return "Hello " . $fname . " " . $sname;
}

// Display hello person message using a function call.
echo helloPerson("Fred","Bloggs");