php - An Error Anonymous functions example -


this question has answer here:

as decided take @ anonymous function, wrote example practice getting error there no warnings found in php storm ide.

whats happening there?enter image description here


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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -