Fixing the “Syntax error, unexpected 'fn' (T_FN), expecting identifier (T_STRING)” PHP error

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.

Learn More about Encora

We are the software development company fiercely committed and uniquely equipped to enable companies to do what they can’t do now.

Learn More

Global Delivery

READ MORE

Careers

READ MORE

Industries

READ MORE

Related Insights

Enabling Transformation in Hospitality through Technology-Led Innovation

As the exclusive sponsor of the 2024 Hotel Visionary Awards, we support organizations leading ...

Read More

Key Insights from HLTH 2024: The Future of Patient-Centered Healthcare

Discover key insights from HLTH 2024 on digital health, AI in diagnostics, data interoperability, ...

Read More

Data-Driven Engineering: Transforming Operations and Products from Insight to Impact

Discover how data-driven engineering transforms operations and product development, enhancing team ...

Read More
Previous Previous
Next

Accelerate Your Path
to Market Leadership 

Encora logo

Santa Clara, CA

+1 669-236-2674

letstalk@encora.com

Innovation Acceleration

Speak With an Expert

Encora logo

Santa Clara, CA

+1 (480) 991 3635

letstalk@encora.com

Innovation Acceleration