0

I have an error with phpstorm when I want to change this function

$callback = create_function('$matches', 'return strtoupper($matches[1]);');

by

callback = function('$matches', 'return strtoupper($matches[1]);');

How to resolve that if it's an error.

Thank you.

4
  • 3
    Define the error. That makes it easier to help you. Commented Oct 27, 2017 at 13:31
  • function() needs to have a body and any argument definitions need to be variables, not strings. The code above is simply invalid PHP. Are you trying to make an anonymous function? If yes, then here's the documentation about those. Commented Oct 27, 2017 at 13:33
  • What version of PHP is your interpreter set to within PHPStorm? Commented Oct 27, 2017 at 13:33
  • 1
    Check the syntax for example #2 on the anonymous function page. You need to tweak the format somewhat. Commented Oct 27, 2017 at 13:34

1 Answer 1

2

You shouldn't use create_function(). create_function() uses eval(). eval() is evil.

On a more serious note, eval() (and thus create_function()) has big security issues. If you're on PHP 5.3 or higher, you should use native anonymous functions instead, in this case:

$callback = function($matches) {
    return strtoupper($matches[1]);
}

For reference: Anonymous functions.

Note that create_function has been deprecated as of PHP 7.2.

Sign up to request clarification or add additional context in comments.

2 Comments

Just for clarify: unlike create_function, the eval is not always evil, but you should have very good reasons for using it (relevant discussion).
@Timurib I agree that eval() isn't always evil, you should just be VERY careful using it whenever user input is involved while using it, or if the user has any chance of manipulating input passend to eval(). If you have the opportunity to use anything else but eval(), use something else. Otherwise, sanitize your inputs :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.