In manual there is create_function function and you can pass result from that function to array_map, I thought that that is the only way to have something like anonymous functions and closures, but then I found that I can just put function like in javascript
array_map(function($a) {
return $a + 1;
}, array(1, 2, 3, 4, 5));
In which version of php I can do this? Was this always there?
Closures (anonymous functions) were added in PHP 5.3.0, including the use clause.
Then since PHP 5.4.0 the static keyword is supported in front of it to denote a static function.
And as of PHP 7.4.0 arrow functions (RFC) as a more concise syntax.
Anonymous functions are available since PHP 5.3:
The key features of PHP 5.3.0 include:
…
Lambda Functions and Closures
…
PHP >5.3:
http://php.net/manual/en/functions.anonymous.php
Related
I have an error with phpstorm when I want to change this function
$callback = create_function('$matches', 'return strtoupper($matches[1]);');
by
callback = function('$matches', 'return strtoupper($matches[1]);');
How to resolve that if it's an error.
Thank you.
You shouldn't use create_function(). create_function() uses eval(). eval() is evil.
On a more serious note, eval() (and thus create_function()) has big security issues. If you're on PHP 5.3 or higher, you should use native anonymous functions instead, in this case:
$callback = function($matches) {
return strtoupper($matches[1]);
}
For reference: Anonymous functions.
Note that create_function has been deprecated as of PHP 7.2.
I have the following code
function MaximArray($arr)
{
$GLOBALS['maxim'] = 0;
array_walk_recursive($arr,create_function('$item,$key','if($item > $GLOBALS["maxim"]) $GLOBALS["maxim"] = $item;'));
return $GLOBALS['maxim'];
}
Why does this function work with $GLOBALS['maxim'] but if I declare a variable as global inside function and use it, doesn't work? I learned that $GLOBALS['a'] and global $a are equal.
Example with global variable $maxim:
$maxim=0;
function MaximArray($arr)
{
global $maxim;
array_walk_recursive($arr,create_function('$item,$key','if($item > $maxim) $maxim = $item;'));
return $maxim;
}
Assuming you're on PHP 5.3, you can use a proper inline function rather than the clunky old create_function() syntax.
array_walk_recursive($arr,function($item,$key) use $maxim {if($item > $maxim) $maxim = $item;}));
Note the use $maxim bit -- this syntax allows you to pass a local variable into an inline function. This is not possible using create_function().
As I said, this is for PHP 5.3 and later. If you're on PHP 5.2 or earlier then the above syntax won't be available to you. However since PHP 5.2 was declared end-of-life over three years ago, if that's the case then you should be urgently considering an upgrade.
I have a certain piece of code that I'm trying to use with PHP Version 5.2.14 . Is it incompatible?? I run the following,
jailshell-3.2$ php -l /XYZ/functions.php
And it gives:
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /XYZ/functions.php on line 2115
Errors parsing /XYZ/functions.php
The code is:
2114 $range = array_map(
2115 function (DatePeriod $p) use ($vt2) {
2116 $res = array();
Your code uses anonymous functions which were supported in PHP 5.3. So, you need PHP 5.3 to get it to working. Upgrade your server's PHP installation.
Anonymous functions, also known as closures, allow the creation of functions which have no specified name.
You are using anonymous functions which are available since PHP 5.3.0.
To resolve this you can upgrade your PHP as suggested in other answer.
Alternatively you can define the function outside array_map and then use that function name in the call to array_map
From the php manual on Anonymous Functions:
Note: Anonymous functions are available since PHP 5.3.0.
prior to 5.3.0, do it like this:
$range = array_map( "name_of_function_to_call", $myArray );
I think the lambda style function is not yet implemented in 5.2
use create_function or just create the function and pass it the function name in array_map.
The following pattern used to be possible in PHP:
function foo($arr)
{
// modify $arr in some way
return $arr;
}
This could then be called using pass-by-value:
$arr = array(1, 2, 3);
$newarr = foo($arr);
or pass-by-reference:
$arr = array(1, 2, 3);
foo(&$arr);
but "Call-time pass-by-reference has been deprecated". Modifying the function signature:
function foo(&$arr)
will handle the pass-by-reference case, but will break the dual-purpose nature of the original function, since pass-by-value is no longer possible.
Is there any way around this?
I think this is as close as you get:
function foo(&$bar) { ... }
foo($array); // call by reference
$bar = foo($_tmp = $array); // call by value
Unfortunately it will require some changes to every call.
The dual-purpose nature was stupid, which is why the behaviour is deprecated. Modify the function signature, as you suggest.
Sadly, I don't believe there is a way around it.
Just make your function use the reference with &.
A simple workaround is to prepare a wrapper function:
function foo($arr) {
return foo_ref($arr);
}
function foo_ref(&$arr) {
...
Then depending on the current use, either invoke the normal foo() or the foo_ref() if you want the array to be modified in place.
There is also a common array(&$wrap) parameter cheat, but that doesn't seem suitable in your case. A more contemporary workaround would be this tricky trick:
// pass by value
foo($array);
// pass by reference (implicitly due to being an object)
foo($array = new ArrayObject($array));
This allows for similar reference passing to unprepared functions. Personally I would prefer keeping the E_DEPRECATED warning and the intended syntax for this purpose.
I have seen the PHP manual. But I don't understand the difference in behaviour between the earlier version and the later versions of PHP. I don't understand this statement:
Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter in versions prior to 5.3.0. If this value must be passed, the results should be assigned to a variable, and that variable should be passed.
If you wanted to pass the result of one of those functions to another function or a method, in versions of PHP prior to 5.3 you had to first assign the result to a variable.
function some_func() {
$args = func_get_args();
some_other_func($args);
}
This limitation was removed in PHP 5.3 and you can now pass the result directly.
function some_func() {
some_other_func(func_get_args());
}
As to why this limitation existed in the first place, perhaps someone with a more thorough understanding of PHP's internals can give you a more complete answer.
It means that this is invalid in 5.2:
function foo() {
$array = array_map('strtolower', func_get_args());
}
foo('BAR', 'BAZ');
It will abort with a Fatal error:
PHP Fatal error: func_get_args(): Can't be used as a function parameter
However in 5.3, it is valid code.