wpseek.com
A WordPress-centric search engine for devs and theme authors



absint › WordPress Function

Since2.5.0
Deprecatedn/a
absint ( $maybeint )
Parameters:
  • (mixed) $maybeint Data you wish to have converted to a non-negative integer.
    Required: Yes
Returns:
  • (int) A non-negative integer.
Defined at:
Codex:
Change Log:
  • 7.2.0

Converts a value to non-negative integer.



Source

function absint( $maybeint ): int {
	if ( is_float( $maybeint ) ) {
		if ( ! is_finite( $maybeint ) ) {
			// Casting `NAN` or `INF` to int has produced `0` since PHP 7.0.
			return 0;
		}

		if ( $maybeint <= (float) PHP_INT_MIN || $maybeint >= (float) PHP_INT_MAX ) {
			// Casting a float beyond the integer range is unreliable and warns as of PHP 8.5.
			return PHP_INT_MAX;
		}
	}

	/*
	 * Casting from an unknown type is the entire contract of this function, so this conversion is
	 * deliberate: arrays, objects, and resources are converted exactly as PHP has always converted
	 * them here, and narrowing the type first would change long-standing behavior. PHPStan flags
	 * such casts for good reason, but that reasoning does not apply here; if the rule level is
	 * ever raised to 9, the resulting `cast.int` error will need to be ignored or baselined.
	 */
	$maybeint = (int) $maybeint;

	if ( PHP_INT_MIN === $maybeint ) {
		// `abs( PHP_INT_MIN )` overflows to a float, as `PHP_INT_MAX` is one less than `-PHP_INT_MIN`.
		return PHP_INT_MAX;
	}

	return abs( $maybeint );
}