wpseek.com
A WordPress-centric search engine for devs and theme authors
rest_is_integer › WordPress Function
Since5.5.0
Deprecatedn/a
› rest_is_integer ( $maybe_integer )
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
| Codex: |
Determines if a given value is integer-like.
This reports whether the value represents an integer; it does not guarantee that the value can be represented as a native PHP integer. Values whose magnitude exceedsPHP_INT_MAX are still reported as integer-like, even though the (int) cast that
rest_sanitize_value_from_schema applies for the 'integer' type cannot round-trip
them: an out-of-range numeric string saturates to PHP_INT_MAX or PHP_INT_MIN, while
an out-of-range float is an undefined conversion in PHP that yields an arbitrary wrapped
value. Likewise, a numeric value with a fractional part that is too large for the fraction
to be represented as a float (greater than 2 ** 53) is reported as integer-like.Related Functions: rest_api_init, rest_is_array, rest_is_object, redirect_this_site, rest_is_ip_address
Source
function rest_is_integer( $maybe_integer ): bool {
if ( is_int( $maybe_integer ) ) {
return true;
}
// A canonical integer string of any magnitude — verified without float conversion.
if ( is_string( $maybe_integer ) && preg_match( '/^\s*[+-]?[0-9]+\s*$/', $maybe_integer ) ) {
return true;
}
// Decimal and scientific-notation strings (and floats) keep their historical behavior.
if ( ! is_numeric( $maybe_integer ) ) {
return false;
}
$float_value = (float) $maybe_integer;
/*
* The strict equality here is not the unreliable "are two computed floats equal" comparison
* (e.g. 0.1 + 0.2 === 0.3, which is false). It compares a float to its own floor() to ask
* "does this float have a fractional part?". A float is whole exactly when it equals its floor,
* so the comparison is exact and safe regardless of floating-point representation error.
*/
return floor( $float_value ) === $float_value;
}