0

Works: returns [false,null]

array_filter([1, 2, 3, false, null], function($value) {
   return empty($value);
});

Does not Work:

array_filter([1, 2, 3, false, null], empty); // syntax error, unexpected ')',expecting '('

Why cant i just pass in the function as an argument?

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Anthony Raimondo
  • 1,621
  • 2
  • 24
  • 40
  • 1
    empty in this case is not a function, please take a look at these answer: http://stackoverflow.com/questions/8399811/array-filter-and-empty. When it is about to call some other functions please go for @arkasch 's advice – swidmann Nov 13 '15 at 16:31

3 Answers3

2

What is empty meant to be in your second example?

Note that php's "empty" is not a function, but a language construct. Therefore you cannot use it as a function. That is explained in the documentation: http://php.net/manual/en/function.empty.php (Scroll down to the "Notes" section...)

That explains why the first variant does work, whilst the second results in a syntax error.

arkascha
  • 41,620
  • 7
  • 58
  • 90
2

Functions can be passed into any function/method that takes a callable argument. However, empty (), in spite of how it's written in code, is not a function. It's a language construct.

If you want to pass empty as a callable, you have no choice but to wrap it in a user-defined function.

$func = function ($param) { return empty ($param); };

print_r (array_filter ([1,2, NULL, 3, 4, FALSE, 5, 6, 0, "", 7, 8, 9], $func));

or

print_r array_filter (([1,2, NULL, 3, 4, FALSE, 5, 6, 0, "", 7, 8, 9], function ($param) { 
    return empty ($param); 
}));

Other language constructs are affected as well, so you might want to consult the manual for a list of language constructs (all the ones that terminate with () like functions are things you can't use with functions that take a callable argument).

http://php.net/manual/en/reserved.keywords.php

GordonM
  • 31,179
  • 15
  • 87
  • 129
1

empty() is not a function, it's a language construct. array_filter expects a callable as the second argument. You will need to use a wrapper function exactly as you have in your first example:

array_filter([1, 2, 3, false, null], function($value) {
   return empty($value);
});
danjam
  • 1,064
  • 9
  • 13
  • 1
    If this does not work, according to your own statement (which is true), then why do you post it as an "answer"? – arkascha Nov 13 '15 at 16:31
  • Because the question was "Why cant i just pass in the function as an argument?" – danjam Nov 13 '15 at 16:32
  • 1
    Yes, true, still this "answer" is very confusing. Stating that this will not work is fine and good, giving a non-working example is a very bad style in my eyes... – arkascha Nov 13 '15 at 16:33
  • You are right, I'll clarify - thank you. – danjam Nov 13 '15 at 16:35
  • 1
    Apart from that the version without quotes would come out the same, since php would assume the non defined constant was meant as a string. So you would get exactly the same result. – arkascha Nov 13 '15 at 16:35
  • Ah, great, you changed your answer. Thanks! – arkascha Nov 13 '15 at 17:07