We have a complete guide with code for fixing the error Function create_function() Has Been Removed in PHP 8.0

There is a common error occurring in PHP 8.0.0, the create_function has been removed. This is a new common error seen by many website owners and developers.

Warning This function has been DEPRECATED as of PHP 7.2.0 and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged.

Why this create_function error is occurring?

You have to know that this function was used to create an anonymous function that is also known as closure, by passing a string of code as an argument.

Instead of using the create_function() you can use anonymous functions, also known as the closures, which were last introduced in PHP 5.3

The create_function has two parameters:

  1. $code – an anonymous function code.
  2. $args – arguments of an anonymous function.

Fix create_function() Has Been Removed in PHP 8.0

For example, the anonymous function has been created using create_function:

<?php

$func = create_function('$x,$y', 'return $x * $y;');
echo $func(4, 5);

You can use an anonymous function like:

<?php

$func = function (int $x, int $y) {
    return $x * $y;
};
echo $func(4, 5);

Or

$add = function($x, $y) { return $x + $y; };

Or this

$add = fn($x, $y) => $x + $y;

Accordingly, it is recommended to use the anonymous functions as they can provide more flexibility, security, and readability compared to create_function()

We hope you find the right answer regarding create_function() and that it helped you.

If have any questions in mind or want to ask something you can ask via the comment section below. We would love to hear from you.

Read More- Most Common WordPress Errors