Articles in Basics Category

PHP Functions

A function is a block of code that you can call throughout your PHP script. Functions are often used for functionalities that can occur more than once in your PHP program.

For an example think that you write a program that relates to seasonal offers. In many occasions, you would need to know the final price after a discount was given. You can define a function for this as below.

function getDiscountedPrice($amount, $percentage=10)
	return $amount-($amount*($percentage/100));

Above function can be called in your PHP script as below.

$discountedPrice = getDiscountedPrice(100, 20);
echo 'Original price is 100.';
echo "Discounted price is $discountedPrice"; // Would print Discounted price is 80

It doesn’t need that the function should be defined before it is called. For instance, in above example function-call can be in an upper part of the script while the function has been defined in a lower part. Once PHP interpreter found a function-call, it would search for the function in current file and in any included file.

You don’t have to assign function-calls to variables. You can directly call functions and their return values will be fetched to the context. For an example, following code behaves same as above.

echo 'Discounted price is ' . getDiscountedPrice(100, 20);

Function Name

Same rules applied for variable names are applied for function names. That is, they can only contain letters, numbers and underscore and, should begin only with a letter or an underscore. Only exception is function names are not case-sensitive. That is, getDiscountedPrice() and GETDISCOUNTEDPRICE() are same.

Since most of the time functions perform a certain action, it’s usual that they associate a relevant verb with function name. For instance, in above example, prefix get has been used instead of naming the function as discountedPrice().


Arguments let you pass different values to a function based on the context. For an example, getDiscountedPrice() wouldn’t be that useful if you couldn’t pass an amount and a percentage to it.

You can define default values to arguments in case that you think it’s not mandatory to pass values for them. For an example think that in your program, you had a default discount of 10% that would be used for most of the products (as shown in above function declaration). Then you can call getDiscountedPrice() as below since you have defined a default value for $percentage.

echo 'Discounted price is ' . getDiscountedPrice(100);  // Would print Discounted price is 90

However you have to be careful on what arguments are optional and their order. For an example, if you had put $percentage as the first argument then you will always have to give a value to it since second argument is mandatory. That means you can’t call the function as getDiscountedPrice( , 100). So, remember to order your arguments in the way mandatory ones come first and then come the optional ones.

Return Value

Return value is the final value you expect from a function after executing code inside it. In above case, it’s the discounted price. You may not expect return values from all the functions. For an example, you can have a function like below to print invoice headers (<br /> tags are used to have line breaks in web browsers).

function printInvoiceHeader()
	echo 'Thank You! <br />';
	echo 'Invoice of XYZ Company <br />';
	echo 'Date: ' . date('Y-m-d');
	// Some more code may go here

printInvoiceHeader(); // This would print out the header

If you don’t specifically return a value in your function then its return value will be NULL. So, if you intended to return a value from your function then always remember to use the return statement.

$invoiceHeader = printInvoiceHeader();
var_dump($invoiceHeader); // Would show that value is NULL.

Remember that return statement is the last statement executed inside a function. Therefore it comes at the end of functions.

function getDiscountedPrice($amount, $percentage=10)
	return $amount-($amount*($percentage/100));
	$specialPrice = $amount-5; // This line won't be executed

Modifying Functions

Think that you want to remove Thank You! part from printInvoiceHeader() and move it to a footer function. You can simply remove corresponding statement and in all the places where printInvoiceHeader() is called, invoice header would be printed without Thank You!.

Ability to make a change and pass that change into many places is a great feature (Think how tedious it could be if you had hard coded invoice header everywhere). However check all the places where the function would be called and see whether change you are going to make is appropriate for all the places.

If you are adding another argument, make sure it has a default value and handle it in a way that won’t give erroneous results to already made function-calls. Otherwise you will have to edit each of those calls to accommodate the change.

Think that you wanted to give a further discount of 20 in some special occasions (amounts greater than 2000, special customer etc) then you could modify getDiscountedPrice() as below.

You would pass true for $specialDiscount when you need the special discount to take place and simply don’t pass any value where it is not necessary. In this way, you don’t have to change already defined function-calls unless the new behavior is required.

function getDiscountedPrice($amount, $percentage=10, $specialDiscount=false)
	$newAmount = $amount-($amount*($percentage/100));

	if ($specialDiscount) {
		$newAmount = $newAmount - 20;

	return $newAmount;

Variable Scope

Arguments and any new variables you define inside the function exists only within the function. For instance, in above example, $amount and $newAmount won’t be available outside (unless they have been specifically defined). This lets you use same variable names for more than one function.

However if you make changes to global variables (like $_SESSION) inside a function then that change would take effect on any other place that uses the particular global variable.

Built-in Functions

One of the cool features of PHP is the huge collection of built-in functions it provides. You can call these functions anywhere in your scripts without defining.

$amount = 537.1267;
$newAmount = round($amount, 2); // $newAmount would be 537.13

Provided your internet connection is enabled, you can get information and usage details of any function by just typing the function name after in your web browser like below.

PHP editors usually have the ability to show built-in functions with an emphasis (with a different color) so you know it’s a built-in one as you type. Make sure you don’t define custom functions with names of built-in functions. For an example, following code block would show you a PHP error.

function round($number, $position)

In additions to built-in functions, PHP offers a set of language constructs (like echo()) which behave similar to functions.

Where to Head from Here...
Share with Your Peers...

We Value Your Feedback...

We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.