wppaste
WordPress

absint( mixed $maybeint ): int

Since
2.5.0
Source
wp-includes/load.php:1468

Casts any value to an integer and strips its sign, guaranteeing a non-negative whole number for things like IDs, counts, and pagination offsets. It truncates decimals during the int cast, so it is not suitable for currency or other values where fractional parts matter. Pair it with intval() when negative numbers are meaningful, or with floatval() when the value can have a decimal component.

Converts a value to non-negative integer.

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$maybeintmixed
Data you wish to have converted to a non-negative integer.

Return value

int
A non-negative integer.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Sanitize a page number from a query string

A pagination link on the front end can be tampered with, so the paged value from the URL needs to become a safe non-negative integer before it is used in a query.

$_GET['paged'] = '-5';

$page = absint( $_GET['paged'] );

printf( 'Raw value: %s, sanitized page: %d', esc_html( $_GET['paged'] ), $page );

The (int) cast inside absint() reads the leading digits of a string, so '5abc' would become 5 before abs() runs.

Normalize a numeric post meta value before displaying it

Post 2 carries a price meta value that a plugin or theme wants to guarantee is never shown as negative.

update_post_meta( 2, 'price', '-19.99' );

$raw_price = get_post_meta( 2, 'price', true );
$safe_price = absint( $raw_price );

printf( 'Raw meta: %s, absint result: %d', esc_html( $raw_price ), $safe_price );

absint() drops everything after the decimal point, so a real currency value like '-19.99' becomes 19, not 19.99; use floatval() plus abs() if the cents matter.

Common problems and fixes · 4

Why does absint() turn my price of 19.99 into 19?

absint() runs (int) $maybeint before abs(), and casting a string or float to int in PHP truncates everything after the decimal point rather than rounding it.

Why does absint('abc') give me 0 instead of an error?

PHP's (int) cast on a non-numeric string like 'abc' resolves to 0, and abs(0) is still 0, so absint() silently swallows bad input instead of flagging it.

Can absint() ever return a negative number?

No. The source wraps the int cast in abs(), so whatever sign the input had, the return value is always zero or positive.

What happens if I pass an array or object into absint()?

The (int) cast on an array collapses it to 1 (non-empty) or 0 (empty) rather than throwing, and casting most objects triggers a fatal error unless the class defines __toString(), neither of which is what a developer usually intends.

Alternatives and related functions

intval
When negative numbers are meaningful and should be preserved rather than forced positive.
floatval
When the value represents a decimal quantity such as a price or weight, since absint() truncates anything after the decimal point.
abs
When the input is already a proper int or float and you only need the sign stripped, without the string-to-int coercion absint() also performs.

Performance profile

How much work a call to absint() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6

Executed per call on PHP 8.5. The body compiles to 6.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost absint() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6abs()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 6 instructions, 6 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 50

Show all 50

Source code

function absint( $maybeint ) {	return abs( (int) $maybeint );}

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/load.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.