I am getting a
Notice: Use of undefined constant ord - assumed 'ord'
On line
array_map(ord,str_split($string))
Which is surprising since ord is a php function
And if I were to rewrite the call as:
array_map(function ($x) {return ord($x);},str_split($string))
The code works without any warning / notices
Any idea why is that ?
Function name should be passed as a string to a callback parameter.
A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().
array_map("ord",str_split($string))
Edit based on edited question.
Anonymous functions are not name of some other callable functions hence they do not need to be passed as string literals. That requirement is only for passing names of defined callable functions, not anonymous functions.
Apart from common user-defined function, anonymous functions can also be passed to a callback parameter.
ord works fine without quotes within the anonymous function because over there it is not being passed as a callable, it is simply being called on a variable within the body of another function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With