# FUNCTIONS

#

The real power of PHP comes from its functions; it has more than 1000 built-in functions.

## PHP User Defined Functions

Besides the built-in PHP functions, we can create our own functions.

A function is a block of statements that can be used repeatedly in a program.

A function will not execute immediately when a page loads.

A function will be executed by a call to the function.

## Create A User Defined Function In PHP

A user defined function declaration starts with the word “function”:

### Syntax

function functionName() {
code to be executed;
}

In the example below, we create a function named “writeMsg()”. The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs “Hello world!”. To call the function, just write its name:

### Example

<?php
function writeMsg()

{
echo “Hello world!”;
}

writeMsg(); // call the function
?>

Output:

Hello world!

## PHP Function Arguments:

Information can be passed to functions through arguments. An argument is just like a variable.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just seperate them with a comma.

The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:

### Example

<?php
function familyName($fname) {
echo “$fname Refsnes.<br>”;
}

familyName(“Jani”);
familyName(“Hege”);
familyName(“Stale”);
familyName(“Kai Jim”);
familyName(“Borge”);
?>

Output:

Jani Refsnes.
Hege Refsnes.
Stale Refsnes.
Kai Jim Refsnes.
Borge Refsnes.

The following example has a function with two arguments ($fname and $year):

### Example

<?php
function familyName($fname, $year) {
echo “$fname Refsnes. Born in $year <br>”;
}

familyName(“Hege”, “1975”);
familyName(“Stale”, “1978”);
familyName(“Kai Jim”, “1983”);
?>

Output:

Hege Refsnes. Born in 1975
Stale Refsnes. Born in 1978
Kai Jim Refsnes. Born in 1983

## PHP Functions – Returning Values

To let a function return a value, use the return statement:

### Example

<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}

echo “5 + 10 = ” . sum(5, 10) . “<br>”;
echo “7 + 13 = ” . sum(7, 13) . “<br>”;
echo “2 + 4 = ” . sum(2, 4);
?>

Output:

5 + 10 = 15
7 + 13 = 20
2 + 4 = 6

PASS BY VALUE & PASS BY REFERENCE

It’s a way how to pass arguments to functions. Passing by reference means the called functions’ parameter will be the same as the callers’ passed argument (not the value, but the identity – the variable itself).Pass by value means the called functions’ parameter will be a copy of the callers’ passed argument.

Pass by reference :

————————————————————————————————————————–

<?php

function disp(&$b)

{

echo $b;

}

$str=”om is good”;

disp($str);

?>

output:

om is good

Pass by value:

————————————————————————————————————————–

<?php

function disp($b)

{

echo $b;

}

$str=”om is good”;

disp($str);

?>

output:

om is good