absint() wordpress function and usage

Home / WordPress / WordPress Wiki / absint() wordpress function and usage

absint( mixed $maybeint )

 

 

This is a WordPress function used in converting a value to non-negative integer.

Parameters

$maybeint

(mixed) (Required) Data you wish to have converted to a non-negative integer.

Return

(int) A non-negative integer.

Usage sample

When passing a negative integer, you’ll get the non-negative, absolute value. Passing a non-int string returns 0, but passing -10, returns 10:

echo absint( 'number' );        // 0
echo absint( 10 );              // 10
echo absint( -10 );             // 10

 

When passing float, it will return integer value

echo absint(20.33);            // 20
echo absint(-20.33);           // 20
echo absint(false);            // 0
echo absint(true);             // 1
echo absint(array(10,20,30))   // 1
echo absint(NULL)              // 0

 

Please note, due to a rounding error (https://bugs.php.net/bug.php?id=33731) you may sometimes get unexpected results for calculations like this:

echo absint( 19.99 * 100 ); // Result is 1998, when the expected result is 1999

 

If you have any question on this function and it usage, kindly use the comment box below this post.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments