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.
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?
- Why does absint('abc') give me 0 instead of an error?
- Can absint() ever return a negative number?
- What happens if I pass an array or object into absint()?
Why does absint() turn my price of 19.99 into 19?
Why does absint('abc') give me 0 instead of an error?
Can absint() ever return a negative number?
What happens if I pass an array or object into absint()?
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
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 6.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | abs() |
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
- Custom_Background::ajax_background_add()Handles Ajax request for adding custom background context to an attachment.
- Custom_Background::wp_set_background_image()
- Custom_Image_Header::ajax_header_add()Given an attachment ID for a header image, updates its "last used" timestamp to now.
- Custom_Image_Header::ajax_header_crop()Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details.
- Custom_Image_Header::ajax_header_remove()Given an attachment ID for a header image, unsets it as a user-uploaded header image for the active theme.
- Custom_Image_Header::get_header_dimensions()Calculates width and height based on what the currently selected theme supports.
- Custom_Image_Header::js_2()Displays JavaScript based on Step 2.
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- Custom_Image_Header::step_3()Displays third step of custom header image page.
- Featured_Content::init()Conditionally hooks into WordPress.
- TwentyTwenty_Walker_Page::start_el()Outputs the beginning of the current element in the tree.
- Twenty_Eleven_Ephemera_Widget::form()Sets up the widget form.
Show all 50
- Twenty_Eleven_Ephemera_Widget::widget()Outputs the HTML for this widget.
- Twenty_Fourteen_Ephemera_Widget::form()Displays the form for this widget on the Widgets page of the Admin area.
- Twenty_Fourteen_Ephemera_Widget::update()Deals with the settings when they are saved by the admin.
- Twenty_Fourteen_Ephemera_Widget::widget()Outputs the HTML for this widget.
- WP_Comment_Query::get_comment_ids()Used internally to get a list of comment IDs matching the query vars.
- WP_Comments_List_Table::__construct()Constructor.
- WP_Comments_List_Table::get_views()Returns an array of comment status links.
- WP_Community_Events::__construct()Constructor for WP_Community_Events.
- WP_Community_Events::cache_events()Caches an array of events data from the Events API.
- WP_Customize_Cropped_Image_Control::to_json()Refresh the parameters passed to the JavaScript via JSON.
- WP_Customize_Header_Image_Control::enqueue()Enqueues control related scripts/styles.
- WP_Customize_Header_Image_Control::render_content()Renders the control's content.
- WP_Customize_Manager::_validate_header_video()Callback for validating the header_video value.
- WP_Customize_Manager::handle_load_themes_request()Loads themes into the theme browsing/installation UI.
- WP_Customize_Manager::register_controls()Registers some default controls.
- WP_Customize_Nav_Menus::ajax_load_available_items()Ajax handler for loading available menu items.
- WP_Customize_Nav_Menus::ajax_search_available_items()Ajax handler for searching available menu items.
- WP_Customize_Nav_Menus::search_available_items_query()Performs post queries for available-item searching.
- WP_HTTP_Requests_Response::set_status()Sets the 3-digit HTTP status code.
- WP_HTTP_Response::set_status()Sets the 3-digit HTTP status code.
- WP_Links_List_Table::prepare_items()
- WP_List_Table::get_pagenum()Gets the current page number.
- WP_Network_Query::get_network_ids()Used internally to get a list of network IDs matching the query vars.
- WP_Privacy_Requests_Table::get_views()Gets an associative array ( id => link ) with the list of views available on this table.
- WP_Privacy_Requests_Table::prepare_items()Prepares items to output.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_Query::parse_query()Parses a query string and sets query type booleans.
- WP_Query::parse_tax_query()Parses various taxonomy related query vars.
- WP_REST_Comments_Controller::get_items()Retrieves a list of comment items.
- WP_REST_Menu_Items_Controller::prepare_item_for_database()Prepares a single nav menu item for create or update.
- WP_REST_Menu_Items_Controller::prepare_item_for_response()Prepares a single nav menu item output for response.
- WP_REST_Pattern_Directory_Controller::prepare_item_for_response()Prepare a raw block pattern before it gets output in a REST API response.
- WP_REST_Users_Controller::delete_item()Deletes a single user.
- WP_REST_Users_Controller::prepare_item_for_database()Prepares a single user for creation or update.
- WP_Roles::for_site()Sets the site to operate on. Defaults to the current site.
- WP_Site_Query::get_site_ids()Used internally to get a list of site IDs matching the query vars.
- WP_Sitemaps::render_sitemaps()Renders sitemap templates based on rewrite rules.
- WP_Term_Query::parse_query()Parse arguments passed to the term query with default query parameters.
Source code
function absint( $maybeint ) { return abs( (int) $maybeint );}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 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.