Skip Navigation

[Resolved] PHP Error

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by andrewD-10 3 years, 11 months ago.

Author
Posts
#1868459

Hi toolset,

I have this custom code inside the toolset "settings" under the custom code tab.

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
function format_my_number($atts)
{
$atts = shortcode_atts(
array('num' => '','sym' => '' ,), $atts );
$num = $atts['num'];
if(number_format($num, 0, '.', ',')==NULL)
{
return number_format($num, 0, '.', ',');
}
else{
return $atts['sym'].number_format($num, 0, '.', ',');
}

}
add_shortcode('format-currency', 'format_my_number');

Unfortunately, I am getting an error

[10-Sep-2020 10:22:56 UTC] PHP Warning: number_format() expects parameter 1 to be float, string given in /home/w2nnms2ait2i/public_html/wp-content/toolset-customizations/currency_format.php on line 14

I am a little confused on how to fix this but I believe it has something to do with how the label is made. This code allows one of my number columns in a toolset field to be displayed in a currency format.

#1868461

Line 14 is: if(number_format($num, 0, '.', ',')==NULL)

#1868705

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The attribute passed to a shortcode is a string.

The PHP function number_format expects the first parameter to be a floating point number, but you are passing a string, hence you see the warning.

hidden link

You can "type cast" the string as a floating point number before using it in the number_format function like so:

$num = (float)$atts['num'];
#1871019

Thank you Nigel, worked perfectly