The most difficult aspect is deciding how to break up programming logic into functions. The mechanics of developing a function in PHP, on the other hand, are quite easy. Just use the function keyword, give it a name, and follow it with parentheses.
function someName ($parameter)
{
$result = 'INIT';
// one or more statements which do something
// to affect $result
$result .= ' and also ' . $parameter;
return $result;
}NULL:function someOtherName ($requiredParam, $optionalParam = NULL)
{
$result = 0;
$result += $requiredParam;
$result += $optionalParam ?? 0;
return $result;
}... followed by a variable name. All parameters supplied will appear as an array in the variable:function someInfinite(...$params)
{
// any params passed go into an array $params
return var_export($params, TRUE);
}function someDirScan($dir)
{
// uses "static" to retain value of $list
static $list = array();
// get a list of files and directories for this path
$list = glob($dir . DIRECTORY_SEPARATOR . '*');
// loop through
foreach ($list as $item) {
if (is_dir($item)) {
$list = array_merge($list, someDirScan($item));
}
}
return $list;
}Usage of the static keyword inside functions has been in the language for more than 12 years. What static does is to initialize the variable once (that is, at the time static is declared), and then retain the value between function calls within the same request.
If you need to retain the value of a variable between HTTP requests, make sure the PHP session has been started and store the value in $_SESSION.
use keyword. The following examples are placed in separate namespaces. Notice that even though the function name is the same, there is no conflict as they are not visible to each other.someFunction() in namespace Alpha. We save this to a separate PHP file, chap_03_developing_functions_namespace_alpha.php:<?php
namespace Alpha;
function someFunction()
{
echo __NAMESPACE__ . ':' . __FUNCTION__ . PHP_EOL;
}someFunction() in namespace Beta. We save this to a separate PHP file, chap_03_developing_functions_namespace_beta.php:<?php
namespace Beta;
function someFunction()
{
echo __NAMESPACE__ . ':' . __FUNCTION__ . PHP_EOL;
}someFunction() by prefixing the function name with the namespace name:include (__DIR__ . DIRECTORY_SEPARATOR
. 'chap_03_developing_functions_namespace_alpha.php');
include (__DIR__ . DIRECTORY_SEPARATOR
. 'chap_03_developing_functions_namespace_beta.php');
echo Alpha\someFunction();
echo Beta\someFunction();Best practice
It is considered best practice to place function libraries (and classes too!) into separate files: one file per namespace, and one class or function library per file.
It is possible to define many classes or function libraries in a single namespace. The only reason you would develop into a separate namespace is if you want to foster logical separation of functionality.
It is considered best practice to place all logically related functions into a separate PHP file. Create a file called chap_03_developing_functions_library.php and place these functions (described previously) inside:
someName()someOtherName()someInfinite()someDirScan()someTypeHint()This file is then included in the code that uses these functions.
include (__DIR__ . DIRECTORY_SEPARATOR . 'chap_03_developing_functions_library.php');
To call the someName() function, use the name and supply the parameter.
echo someName('TEST'); // returns "INIT and also TEST"You can call the someOtherName() function using one or two parameters, as shown here:
echo someOtherName(1); // returns 1 echo someOtherName(1, 1); // returns 2
The someInfinite() function accepts an infinite (or variable) number of parameters. Here are a couple of examples calling this function:
echo someInfinite(1, 2, 3); echo PHP_EOL; echo someInfinite(22.22, 'A', ['a' => 1, 'b' => 2]);
The output looks like this:

We can call someInfinite() as follows:
foreach (someDirScan(__DIR__ . DIRECTORY_SEPARATOR . '..') as $item) {
echo $item . PHP_EOL;
}