## Topic outline

• ### PHP – Functions

• Lesson

A function is a named block of code that performs a specific task, possibly acting upon a set of values given to it, aka parameters, and possibly returning a single value or set of values in an array. Functions save on compile time—no matter how many times you call them, functions are compiled only once for the page.

Functions have many advantages over contiguous, inline code. For example, they:

• Involve less typing

• Reduce syntax and other programming errors

• Decrease execution time, because each function is compiled only once, no matter how often you call it

• Accept arguments and can therefore be used for general as well as specific cases

SYNTAX TO DECLARE A FUNCTION

function functionName()

{
code to be executed;
}

A definition starts with the word function. A name follows, which must start with a letter or underscore, followed by any number of letters, numbers, or underscores. The parentheses are required. One or more parameters, separated by commas, are optional.

The following simple program illustrates function-Adding parameters

<html>

<body>

<?php

// Function definition     function writeName($fname) { echo$fname . " Refsnes.<br />";

}

echo "My name is ";      writeName("Kai Jim"); //Function call

echo "My sister's name is ";      writeName("Hege"); // Function call

echo "My brother's name is ";      writeName("Stale"); // Function call

?>

</body>

</html>

OUTPUT OF THE ABOVE GIVEN EXAMPLE IS AS FOLLOWS:

My name is Kai Jim Refsnes.

My sister's name is Hege Refsnes.

My brother's name is Stale Refsnes.

PHP FUNCTION-RETUNING VALUES

The following code illustrates Returning values of a function

<html>

<body>

<?php

// Function definition

function add($x,$y)

{

$total=$x+$y; return$total;

}

// Function call

echo "1 + 16 = " . add(1,16);

?>

</body>

</html>

OUTPUT OF THE ABOVE GIVEN EXAMPLE IS AS FOLLOWS:

1 + 16 = 17

RETURNING AN ARRAY

There are also ways of getting multiple values from a function. The first method is to return them within an array.

The following PHP code illustrates Returning multiple values in an array

<?php

$names = fix_names("WILLIAM", "henry", "gatES"); echo$names[0] . " " . $names[1] . " " .$names[2];

function fix_names($n1,$n2, $n3) {$n1 = ucfirst(strtolower($n1));$n2 = ucfirst(strtolower($n2));$n3 = ucfirst(strtolower($n3)); return array($n1, $n2,$n3);  }

?>

This method has the benefit of keeping all three names separate, rather than concatenating them into a single string, so we can refer to any user simply by first or last name, without having to extract either name from the returned string.

Do Not Pass Arguments by Reference: In PHP versions prior to 5.3.0, we used to be able to preface a variable with the & symbol to tell the parser to pass a reference to the variable, not the variable’s value. Passing by reference was deprecated in PHP 5.3.0 and was removed in PHP 5.4.0. we should therefore not use this feature other than on legacy websites.

RETURNING GLOBAL VARIABLES

The better way to give a function access to an externally created variable, is by declaring it a global variable from within the function. The global keyword followed by the variable name gives every part of your code full access to it.

<?php

$a1 = "WILLIAM";$a2 = "henry";

$a3 = "gatES"; echo$a1 . " " . $a2 . " " .$a3 . "<br>";

fix_names();  echo $a1 . " " .$a2 . " " . $a3; function fix_names() { global$a1; $a1 = ucfirst(strtolower($a1));

global $a2;$a2 = ucfirst(strtolower($a2)); global$a3; $a3 = ucfirst(strtolower($a3));

?>

Now we don’t have to pass parameters to the function, and it doesn’t have to accept them. Once declared, these variables remain global and available to the rest of your program, including its functions.