Skip Navigation

[Resolved] Fatal error of snippet on PHP8

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by nicolaS-3 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2516271
fatalerror.jpg

Hi all
some time ago you helped me creating a snippet that allowed me to convert currency values from US to EU format. This is the function:

function format_my_number($atts)
{
$atts = shortcode_atts(
array(
'num' => '',
'sym' => '',
),
$atts
);
$num = $atts['num'];

return number_format($num, 0, ',', '.').' '.$atts['sym'];
}
add_shortcode('format-currency', 'format_my_number');

I was recently required to move my site to PHP 8 and I now get the fatal error attached from the function (that still works fine on frontend, but it fails when editing the page).
Could you please help me to fix the issue ?
thanks
Regards
Nicola

#2516715

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to use the following function. It seems with PHP 8 it has a strict type casting for variables.

Could you please try to use the following code and check if that help you to resolve your issue:

function format_my_number($atts)
{
$atts = shortcode_atts(
array(
'num' => '0',
'sym' => '',
),
$atts
);
$num = $atts['num'];
$num = floatval($num);
return number_format($num, 0, ',', '.').' '.$atts['sym'];
}
add_shortcode('format-currency', 'format_my_number');
#2516849

My issue is resolved now. Thank you!