Thursday, 19 May 2011

PHP FUNCTIONS - WHY PHP FUNCTIONS ARE USEFUL




The PHP programming language proposes a set of predefined functions for you to accomplish the most simple tasks.

For instance, when you want to print the content of a PHP variable $var, you can use the print() function. Such a task is so elementary that not providing a specific PHP command for it would be ridiculous; conversely, it would be equally nonsensical to provide predefined PHP functions for every possible sequence of PHP commands, since such functions would hardly be used because they are based on very particular web development needs.

As a web developper, you are thus able to define your own functions in PHP, that is to say sequences of PHP statements which can accept parameters and can be called from different places of a script in order to complete a defined set of tasks whenever you need it.

The reasons you can have for defining a PHP function are severalfold:
  • There is a sequence of PHP commands that you will need to reuse multiple times in your script(s).
  • A sequence of commands that needs to be executed in your PHP script requires a common set of client-defined parameters.

PHP FUNCTIONS - HOW TO DEFINE A FUNCTION IN PHP


The PHP syntax to define a function is rather easy to remember:

function function_name($par_1, ... , $par_n)
{
php_instructions
}
  • where function_name is the name of your function (a PHP function name must have the same form as a PHP variable name, the only difference being that you must not put a dollar symbol in front of it)
  • where the $par_i denote parameters that are accepted by the function (its arguments, in other words)
  • where php_instructions is the set of PHP instructions that must be executed by the function.


Remarks:

  • Unlike PHP variable names, PHP function names are case insensitive. It is however recommended to remain rigorous when naming and calling any PHP object (remember what we said about how easy it is to make errors in loosely typed programming languages).
  • A function can be defined within another function.
  • Every function has a global scope (even if it was defined within another function).



PHP FUNCTIONS - RECURSIVE PHP FUNCTIONS



PHP allows the definition of recursive functions, that is to say functions that call themselves within their own definition in order to get evaluated by induction. An example of recursive PHP function is provided below:

Learn the PHP code:
<?php
function cumulative_sum($number)
{
$result = 0;
if ($number > 0)
$result = $number + cumulative_sum($number-1);
return ($result);
}
?>

Run the PHP script in your web browser:

Recursive functions in PHP

Remark:

The return() statement ends the execution of the function and returns the value of the function call (which is taken as an argument of return()).

PHP functions allow you to group a particular set of PHP commands which require a common set of arguments; in this way, a PHP function can be called multiple times within your script, or even be exported to other scripts by using the include() statement (or the require() statement). PHP functions constitute the building blocks of your applications.

No comments:

Post a Comment