Fixed Template variable filters which are not php 8.1 safe


Well-known member
Affected version
2.2.8 Patch 1
The following snippet (more realistically from variable usage) will cause a warning in php 8.1+
{{ null|file_size }}

Will cause this error:
Template error: [E_DEPRECATED] number_format(): Passing null to parameter #1 ($num) of type float is deprecated

This affects a lot of the filters which handle stringy input that rattles down to an internal php function.

Test cases;
{{ null|file_size }}
{{ null|format }}
{{ null|hex }}
{{ null|host }}
{{ null|ip }}
{{ null|number_short }}
{{ null|numeric_keys_only }}
{{ null|pad(null,null) }}
{{ null|split }}
{{ null|strip_tags }}
{{ null|to_lower }}
{{ null|to_upper }}
{{ null|de_camel }}
I could be wrong, but I think in most cases passing null to these would be unintentional and a sign of a possible bug. Personally, I'd be in favor of deliberately throwing an exception in older PHP versions when null is passed, but perhaps that would be too harsh. Maybe it could throw an exception in debug mode for the next few versions to give add-on devs time to prepare.

To the make the transition easier for any affected add-ons, it would also be handy to introduce a null coalescing operator or filter. Any add-ons that use such a feature would be incompatible with previous XF versions, so it makes sense to implement it sooner rather than later, even if the feature remains undocumented for a few versions.
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.9).

Change log:
Improve PHP 8.1 compatibility within template filters
There may be a delay before changes are rolled out to the XenForo Community.
Top Bottom