Skip to main content
search

© 2021 Excellerate. All Rights Reserved

In this short post, we show to fix the “unexpected error ‘fn'” in The recent PHP upgrade to version to 7.4.x with WordPress version 5.5.3 leads to the following error in different plugins resulting in an application crash.

Parse error: syntax error, unexpected ‘fn’ (T_FN), expecting identifier (T_STRING) in xxxx.php file line number 121.

The genesis of this error

This error results in the newer version of PHP 7.4.x as “fn” is reserved as a PHP keyword.

The latest version of PHP, 7.4, introduces arrow functions. Initially anonymous functions are fairly quite difficult to implement and maintain. However arrow functions allow us to clean up our PHP code.

Both anonymous functions and arrow functions are implemented using the Closure class.

Arrow functions support the same features as anonymous functions, with the exception of automatic usage of variables from its parent scope.

Here is an example of arrow functions,

<?php
$y = 4;
$fnAdd = fn($x) => $x + $y;
// equivalent to using $y by value:
$fnEquivalent = function ($x) use ($y)
{
   return $x + $y;
};
var_dump ($fnAdd (3));
?>
// The output is 7

If you take a closer look at the example above, you’ll notice that the “fn” is the PHP keyword which is used in the arrow functions.

People suggest getting around this issue by downgrading to an earlier PHP version while some say it is a bug. However, there is no need to downgrade the PHP version and it is not a bug. Listed below is a solution to fix the same:

To fix this issue you simply need to change instances of “fn” to “fn1” or “fn2”. 

The choice of names is entirely up to you. I also found this error in several WordPress plugins as well. You can edit the plugin files and change the function name i.e. “fn” to something else.

Hopefully as plugins get upgraded, they will resolve these issues.

Leave a Reply