1.2.94. Callback Function Needs Return¶
When used with array_map() functions, the callback must return something. This return may be in the form of a return
statement, a global variable or a parameter with a reference. All those solutions extract information from the callback.
The following functions are omitted, as they don’t require the return :
<?php
// This filters each element
$filtered = array_filter($array, function ($x) {return $x == 2; });
// This return void for every element
$filtered = array_filter($array, function ($x) {return ; });
// costly array_sum()
$sum = 0;
$filtered = array_filter($array, function ($x) use (&$sum) {$sum += $x; });
// costly array_sum()
global $sum = 0;
$filtered = array_filter($array, function () {global $sum; $sum += $x; });
// register_shutown_function() doesn't require any return
register_shutown_function("my_shutdown");
?>
See also array_map.
1.2.94.1. Connex PHP features¶
1.2.94.1.1. Suggestions¶
Add an explicit return to the callback
Use null to unset elements in an array without destroying the index
1.2.94.1.2. Specs¶
Short name |
Functions/CallbackNeedsReturn |
Rulesets |
|
Exakat since |
1.2.6 |
PHP Version |
All |
Severity |
Major |
Time To Fix |
Instant (5 mins) |
Precision |
High |
Examples |
|
Available in |