The recommended way to do this is with an anonymous function:
$functions = [
'function1' => function ($echo) {
echo $echo;
}
];
If you want to store a function that has already been declared then you can simply refer to it by name as a string:
function do_echo($echo) {
echo $echo;
}
$functions = [
'function1' => 'do_echo'
];
In ancient versions of PHP (<5.3) anonymous functions are not supported and you may need to resort to using create_function (deprecated since PHP 7.2):
$functions = array(
'function1' => create_function('$echo', 'echo $echo;')
);
All of these methods are listed in the documentation under the callable pseudo-type.
Whichever you choose, the function can either be called directly (PHP โฅ5.4) or with call_user_func/call_user_func_array:
$functions'function1';
call_user_func($functions['function1'], 'Hello world!');
Answer from Alex Barrett on Stack OverflowCan you store a function in a PHP array? - Stack Overflow
What kind of array does PHP use? - Stack Overflow
Do yourself a favor and learn all of the array functions
Is there a built-in PHP function that can take any multidimensional array and replace specific values?
Videos
The recommended way to do this is with an anonymous function:
$functions = [
'function1' => function ($echo) {
echo $echo;
}
];
If you want to store a function that has already been declared then you can simply refer to it by name as a string:
function do_echo($echo) {
echo $echo;
}
$functions = [
'function1' => 'do_echo'
];
In ancient versions of PHP (<5.3) anonymous functions are not supported and you may need to resort to using create_function (deprecated since PHP 7.2):
$functions = array(
'function1' => create_function('$echo', 'echo $echo;')
);
All of these methods are listed in the documentation under the callable pseudo-type.
Whichever you choose, the function can either be called directly (PHP โฅ5.4) or with call_user_func/call_user_func_array:
$functions'function1';
call_user_func($functions['function1'], 'Hello world!');
Since PHP "5.3.0 Anonymous functions become available", example of usage:
note that this is much faster than using old create_function...
//store anonymous function in an array variable e.g. $a["my_func"]
$a = array(
"my_func" => function($param = "no parameter"){
echo "In my function. Parameter: ".$param;
}
);
//check if there is some function or method
if( is_callable( $a["my_func"] ) ) $a["my_func"]();
else echo "is not callable";
// OUTPUTS: "In my function. Parameter: no parameter"
echo "\n<br>"; //new line
if( is_callable( $a["my_func"] ) ) $a"my_func";
else echo "is not callable";
// OUTPUTS: "In my function. Parameter: Hi friend!"
echo "\n<br>"; //new line
if( is_callable( $a["somethingElse"] ) ) $a"somethingElse";
else echo "is not callable";
// OUTPUTS: "is not callable",(there is no function/method stored in $a["somethingElse"])
Since PHP 7.4 there are also "Arrow functions"
Arrow functions provide a shorthand syntax for defining functions with implicit by-value scope binding.
It's very similar to the "classic" anonymous function, however it supports only 1 expression and not a block of commands - often used for quick filtering and comparing:
E.g. to get a list of IDs from a book object list:
$extractIds = fn (object
o->getId();
$bookIds = array_map($extractIds, $books);
A great use with spaceship (<=>) operator:
$someArray = [2,3,1];
$sort = [
SORT_ASC => fn(
b) =>
b,
SORT_DESC => fn(
b) =>
a,
];
usort($someArray, $sort[SORT_ASC]); // someArray will contain: 1, 2, 3
usort($someArray, $sort[SORT_DESC]); // someArray will contain: 3, 2, 1
Constants note: So far (PHP8.2, 2023) PHP doesn't allow storing anonymous functions (nor arrow versions) as constants.
REFERENCES:
Anonymous function: https://www.php.net/manual/en/functions.anonymous.php
Arrow function: https://www.php.net/manual/en/functions.arrow.php
Arrow function (release notes): https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.arrow-functions
Test for callable: https://www.php.net/is_callable
PHP is not as strict as C or C++. In PHP you don't need to specify the type of data to be placed in an array, you don't need to specify the array size either.
If you need to declare an array of integers in C++ you can do it like this:
int array[6];
This array is now bound to only contain integers. In PHP an array can contain just about everything:
$arr = array();
$arr[] = 1;
$arr[] = 2;
$arr[] = 3;
$arr[] = 4;
var_dump($arr); //Prints [1,2,3,4]
$arr[] = 'hello world'; //Adding a string. Completely valid code
$arr[] = 3.14; //Adding a float. This one is valid too
$arr[] = array(
'id' => 128,
'firstName' => 'John'
'lastName' => 'Doe'
); //Adding an associative array, also valid code
var_dump($arr); //prints [1,2,3,4,'hello world',3.14, [ id => 128, firstName => 'John', lastName => 'Doe']]
If you're coming from a C++ background it's best to view the PHP array as a generic vector that can store everything.
From php.net
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.