Begin Web Programming with PHP & MySQL

Advertisements



PHP Functions

A function is a block of code that can be reused many times. It can take input as an argument list and return value. Function can be considered as a "self contained" module of code that accomplishes a specific task.

When a function is called the program leaves the current section of code and begins to execute code inside the function. After the execution of the function control returns to the program which has been paused to execute the function. There are thousands of in-built functions in PHP. Users can define new functions too.

Advantages of PHP functions
  1. They allow us to break down our program to small blocks of codes.
  2. They allow us to reuse code instead of rewriting it.
  3. It is easier to understand the flow of the program because every logic is divided in the form of functions.
  4. Functions allow us to test small parts of our program in isolation from the rest.
PHP user defined functions

We can create our own functions in PHP. A function name must start with a letter or an underscore.

There are two ways to create an associative array:
Syntax:
function function_name()
{
//code to be executed;
}

Note that when creating a function, the name of the function should start with the keyword 'function' and all the code should be put inside curly braces ({ }).

Eg:
<?php
function firstFunc()
{
echo "Hello world.";
}
firstFunc(); // calling the function
?>
Output will be
Hello world.

Function with Arguments

Parameters are the variables contained within the function's parenthesis. Parameter variables are used to import arguments into functions. These are used to store the values that can be executed at runtime. Arguments are listed within parentheses after the function name. You can add as many arguments as you want; just use a comma to divide them.

Eg:
<?php
function fruits($fruit1,$fruit2) // function with parameters $fruit1 and $fruit2
{
echo "Passed fruit names are: ".$fruit1." and ".$fruit2;
}
fruits("apple","orange"); // calling function with arguments passed
?>

Output:
Passed fruit names are: apple and orange

Default Argument Value

A function may be defined with default values for arguments using syntax similar to assign a variable. Default value will be used only when the argument is not specified.

Following example shows the use of a default parameter. If we call the function setHeight() without arguments it takes the default value as argument.

Eg:
<?php
function fruit_colour($fruit,$colour = "red")
{
echo "The ".$fruit." has the colour ".$colour;
echo "<br>";
}
fruit_colour("Apple"); // takes default argument - red
fruit_colour("Orange","orange"); // uses the passed argument - orange
?>

Output:
The Apple has the colour red
The Orange has the colour orange

Passing Arguments by Reference

By default, function arguments are passed by value. To allow a function to modify its arguments, they must be passed by reference. In order to receive arguments by reference, variables used for the parameters must be prefixed by '&'.

Eg:
<?php
function fruit_exchange(&$fruit1, &$fruit2)
{
$temp = $fruit1;
$fruit1 = $fruit2;
$fruit2 = $temp;
}
$fruit1 = "Apple";
$fruit2 = "Orange";
echo "Before calling function : fruit1 = $fruit1 and fruit2 = $fruit2<br>";
fruit_exchange($fruit1, $fruit2);
echo "After calling function : fruit1 = $fruit1 and fruit2 = $fruit2<br>";
?>

Output:
Before calling function : fruit1 = Apple and fruit2 = Orange
After calling function : fruit1 = Orange and fruit2 = Apple