PHP functions

Website Theme

PHP functions

August 29th, 2012

A good programming practice is called Do not Repeat Yourself (DRY). The idea is that you attempt to maximize code reuse. One way to do this is to write functions. A function is a block of code that you write once and then you can execute it multiple times from other parts of your code. Executing a function is called “calling” it. PHP allows you to write functions quite easily.

A PHP function begins with the function keyword followed by a function identifier, the parameter list and an opening brace:

function foo( $variable ) {
//body of function

In the above example the function is named foo and has one parameter called $variable. A parameter is a variable that stores a value that a function will accept from the calling code. You can have multiple parameters separated by commas. Parameter variables are “local” to the function i.e. they are not accessible from outside the function.

After the opening brace you have the body of the function which is the business end of the function. The function ends with a closing brace.

Calling a function

Calling the foo function given above is easy:

foo( "elephant" );

Simply place the function identifier followed by opening brackets and the arguments (values) you want to pass to the function and a closing bracket.

Example function

Let’s look at an example function:

function foobar( $statement ) {
 echo $statement;
foobar( 'Hello World' );

In the above example you have a function foobar that prints the its only parameter. So when you run the above code it will print the “Hello World” that you called it with.

Function returns

A function can also return a value to the calling code. It does that with the return statement:

function foobarbaz( $num1, $num2 ) {
 return $num1 * $num2;
$number = 3;
$return_value = foobarbaz( $number, 5 );
echo $return_value;

In the above example the function foobarbaz accepts two values that are then multiplied and returned to the calling code. The calling code stores the return value in the variable $return_value and then displays it. So the above code will display ’15’.

Optional parameters

A function can have optional parameters that can be omitted in the function call. These parameters then take the default value you specify for them in the function declaration:

function optional_par( $num1, $num2, $num3 = 56 ) {
 echo $num1 * $num2 * $num3;
optional_par( 5, 4 );

In the above example $num3 is the optional parameter with the default value of 56. Optional parameters are always right most in the function declaration. The above code will display 5*4*56 = 1120. If it were called with optional_par( 5, 4, 3 ) it would display 5*4*3 = 60 instead.

Final thoughts

Functions are like the basic building blocks of a PHP application. You can divide a large program into small functions that are easy to code and help you build up to your complete application.

Comments are closed.

Domain for sale

This domain name is for sale. If you are interested in buying it please contact me.