Verification: a143cc29221c9be0

Php call function before function

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

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 (
tags are used to have line breaks in web browsers).

function printInvoiceHeader()
{
	echo 'Thank You! 
'; echo 'Invoice of XYZ Company
'; 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.