Skip Navigation

[Resuelto] PHP Error

This support ticket is created hace 3 años, 4 meses. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Etiquetado: 

This topic contains 3 respuestas, has 2 mensajes.

Last updated by andrewD-10 hace 3 años, 4 meses.

Autor
Mensajes
#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: Inglés (English ) Español (Español )

Timezone: Europe/London (GMT+01: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

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.