Popular Blog Posts on The Project Shrink

25 Sure-fire Ways To Motivate Your Team Members

23 Powerful Tips for Working in Multi-Cultural Teams

PHP Functions

Functions are the most important part of any programming language, PHP included. You can say in a few words that functions are pieces of code that accept values and produce results. While there are functions that you don't need to supply any values to, a function which does nothing is pointless. Functions come in handy when you're writing repetitive code, and you're looking to use the same code in other scripts.

print "Welcome to my web-page!";

A function is a block of code that is not immediately executed, but can be called by your scripts when needed. Functions can either be built-in or defined by the user. PHP has a lot of built-in functions; one of them is "print()". In the previous examples we used it to output strings to the web-browser. The values used along with the function are called arguments. Arguments are included in the parentheses of a function call, but this doesn't always apply. For some build-in functions, for example "print()", you can choose to use them or not use them, PHP will accept it either way.

Different functions require different arguments; functions may ask for multiple arguments (which are separated by commas), but on the other hand, they could also be executed without any arguments, in which case you only include the parenthesis after the function name. It all depends on the function and what it does. For example, a function that calculates the values of a purchase based on the price and the number of purchased items will require two values. Let's assign the result to a variable:

$value = calculate_value($item_price, $number_of_items); //calls the "calculate_value()" function with two arguments

As you can see, functions require data to be passed to them, and then return a result based on that data. Most functions give you some information after their completion, even if that only tells you if their mission was successful (a boolean type result). This is not always the case. You don't have to use any arguments when you use a simple function that outputs the same message everytime it is called, for example "Thanks for visiting our web-site", and it also doesn't return any result. When declaring a function, note that you must not add a semicolon after the function statement:

function display_goodbye_message() //no semicolon here!

{

print "<h1>Thanks for visiting our web-site</h1>";

}

This will create a function that, whenever called, will output the same string. It requires no arguments, and it doesn't return anything. This isn't so useful, and you probably won't see many similar functions. Let's get back to our value calculating function. As you can see, you can use the "return" statement to assign a result to the function – in this case, the result will be the calculated value of the purchase (the price multiplied by the number of items purchased). This is how you define it:

function calculate_value($price, $items)

{

return ($price * $items); //returns a result

}

We can call functions dynamically the same way you can access dynamic variables. This means that you can treat an expression's name as a function name, and thus call it.

$function_name = "calculate_value";

$function_name(40, 5);

This is identical to:

calculate_value(40, 5);

When a function uses a variable, there are several issues you should be aware of. It's important to know that if you declare a variable inside a function, that variable will only be available to the rest of that function's code. This is called a local variable. If you try to use this variable in another function, it will not hold any value.

But sometimes you want to access some important information outside the function, without using an argument. One way to do this, you must the "global" statement.

$exchange_rate = 1.27;

function calculate_value_from_foreign($price, $items)

{

global $exchange_rate; //make "$exchange_rate" available to the function

return ($price * $items * $exchange_rate);

}

We expanded the previous script a little bit. You can see how we use "global" to inform the function that the "$exchange_rate" variable has been declared outside it. If we have hadn't done this (try to comment the line), the function would have returned zero, because "$exchange_rate" would have been "NULL". The function simply doesn't know the value of "$exchange_rate" if you don't tell it that it was assigned outside the function. Be careful, because if you modify the value of a variable within a function, that value will be changed for the following code after the call to the function; PHP uses the variable itself, and not a copy.

Another way to access the data outside the function, without using an argument is by using a reference to a variable as an argument. You can do this by placing an ampersand "&" in front of the argument, when you define the function. This way, the function will directly access the variable through the reference, and the variable can be assigned and read at the same time.

$exchange_rate = 1.27;

function add_tax_and_calculate_value_from_foreign($price,
$items, &$rate) //notice the ampersand "&", telling
PHP to treat the variable "$rate" as a reference

{

$rate .= 0.01; //adds a 0.01 tax

return ($price * $items * $exchange_rate);

}

add_tax_and_calculate_value_from_foreign(15, 4, $exchange_rate)

print $exchange_rate; //the new exchange rate is 1.28, because the function has incremented it

As you can see, PHP is very versatile. You can define functions within other functions, you can define a function based on a condition, you can use a variable number of arguments, you can use a default argument, and many more. Functions don't act like variables; that is, you don't have to first declare it then use it. You can declare a function and the very end of your script, and use it at the beginning, even though this is really not recommended. Before starting the actual execution of the script, PHP searches the script for functions, so it will know where to look for if you call one.

  • hai,
    you are tutorial is exalent....................for
    PHP.
blog comments powered by Disqus