I see you’ve found your own answer, but I wanted to offer some advice for making your code a little more succinct.
<?php
// Different versions of a number
$unformatted = get_field('paid_attendance');
$cleaned = str_replace(',', '', $unformatted); // Remove commas from string
$integerValue = absint( $cleaned ); // Convert to absolute integer.
$floatValue = number_format($cleaned, 2); // Apply number format.
// Note that number_format returns a string, so if you want
// an actual float, call floatval _now_:
$floatValue = floatval( $floatValue );
if ( 0 < $integerValue ) {
echo $integerValue;
}
So, a few things to unpack here:
- You can use the “cleaned” (comma-removed) string for getting both your integer and float
absint
is a WordPress function that will convert a value to an absolute integer.number_format
returns astring
, so calling it afterfloatval
orintval
was superfluous.- Your initial
if
statement didn’t do anything, so you can omit it. - You’re better off checking the
$integerValue
as it’s anint
instead of$unformatted
or$cleaned
since you’re doing a numeric comparison.- Suppose
$unformatted
were the string ‘a’. In PHP, zero is not less than ‘a’:
- Suppose
php > var_dump( 0 < 'a' );
bool(false)