PHP Functions

PHP provides many built in functions such as sort() function. A function is simply blocks of code that are executed when you call that function. This saves you from repeating code (DRY = Don’t Repeat Yourself), which makes code more readable and allows you to organise it more efficiently.

Syntax

function function_name()
{
code to be executed
}

Function Naming Rules

  • The function name must begin with a letter or underscore
  • The function can contain letters, numbers and underscores
  • Function names are case-sensitive so Asim and asim are two totally different functions
  • You cannot duplicate function names

Create a new PHP file and name it functions.php. Next, copy this code:

<?php

function myFunction(){
echo 'Hello World';
}

myFunction();

?>

This is a very simple function; it simply prints out “Hello World” to the browser. The function is called via its name followed by parentheses and semi-colon.

Variables In Functions

You can have variables in functions, however there are some rules you must follow:

  • Variables created outside a function have global scope
  • Variables created inside a function have local scope
  • Global variables cannot be used inside a function
  • Local variables cannot be used outside a function

Since local variables can only be used inside a function, you can duplicate the variable name and use them in other functions without them conflicting.

Example

<?php

$myNumber = 10;

function vFunction()
{
$myNumber = 5 * 10;
echo $myNumber;
}

vFunction();
echo $myNumber; 

?>

Here there are two variables named myNumber: the first one is global and the second one is local, and they will not conflict since they are of different scope.

Ouput

50 // 5* 10 = 50
10 // (Global Variable)

Passing Arguments

Arguments (or parameters) are data passed to functions (in the parentheses). When you call that function you specify the arguments, see the examples below.

Syntax

<?php

function functon_name ($argument, $argument)
{
code to be executed
}

?>

Multiple arguments are separated by commas.

Example

<?php

function _Information($age, $firstname, $lastname)
{
 echo "Welcome $firstname $lastname, you are $age years old.";
} 

_Information(18, 'Asim', 'Rehman'); 

?>

In this example there are 3 arguments: $age, $firstname, and $lastname. When we call the function we need to pass the 3 arguments and their data types are: integer, string, and string. Argument variables also have local scope so you can use them again in other functions without conflict.

Example – VAT Calculator

This example is similar to the one above, except this time we have a global variable. You calculate the VAT using the percentage multiplier. The current VAT rate in the UK is 20%, so (100 + 20) / 100 = 1.2. 1.2 * Price ex.VAT = totalcost. We have brackets around the 100 and VRate because we want this calculation to be done first.

<?php

$VRate = 20;

function CalculateVAT($cost, $VRate)
{

 $multiplier = (100 + $VRate) / 100;
 $totalcost = $cost * $multiplier;
 echo "The total product cost is ".$totalcost;
}

CalculateVAT(709, $VRate);

?>

When you call the function you also need to pass the arguments. Here we used the global variable $VRate which is set to 20.

Returning Values

Some built in functions return values. In the previous example, we could have simply returned the total cost using the return keyword. Typically this goes at the end, like this:

<?php

$VRate = 20;

function CalculateVAT($cost, $VRate)
{

 $multiplier = (100 + $VRate) / 100;
 $totalcost = $cost * $multiplier;
 return $totalcost;
}

echo CalculateVAT(709, $VRate); 

?>

Here we used the return keyword to return the total cost. When we call the function we used the echo command to print out to the screen, since the return does not echo out the result.

Summary

PHP functions are helpful and are a good way to maintain your code. There are many built in functions in PHP and it is easy to create your own functions. Remember that variables in functions have local scope and thus can be used in other functions without conflicting. Variables that are made outside functions have global scope and cannot be used inside functions. Arguments (or parameters) can be passed to functions and are separated by commas for more than one argument.

  • Functions must start with underscore or letter
  • Functions can contain underscore, letters and numbers
  • You cannot duplicate a function name