php - An Error Anonymous functions example -
this question has answer here:
- php parse/syntax errors; , how solve them? 11 answers
as decided take @ anonymous function, wrote example practice getting error there no warnings found in php storm ide.
whats happening there?
actual code
$functioninstant = create_function('$a,$b', 'return (strlen($a) - strlen($b))'); $list = array('really long string', 'this', 'middle', 'large'); usort($list, $functioninstant); print_r($list);
error message
parse error: syntax error, unexpected '}' in functions.php(87): runtime-created function on line 1
warning: usort() expects parameter 2 valid callback, no array or string given in functions.php on line 89
assuming moderately recent version of php, use anonymous function, eg
usort($list, function($a, $b) { return strlen($a) - strlen($b); });
your actual error because missing semi-colon in function body string. should be
$functioninstant = create_function('$a,$b', 'return strlen($a) - strlen($b);');
Comments
Post a Comment