absint()
概述
absint() 是 WordPress 核心函数,用于将任意值转换为非负整数。它通过强制类型转换和取绝对值操作,确保返回结果始终为非负整数,常用于数据验证和清理场景。
关键要点
- 函数接受一个参数 $maybeint,可以是任意类型的数据,返回一个非负整数。
- 内部实现为 abs((int) $maybeint),先强制转换为整数,再取绝对值。
- 广泛用于 WordPress 核心代码中,涉及用户查询、REST API、媒体处理、数据库操作等多个模块。
- 对于浮点数、布尔值、数组、NULL 等特殊输入,转换规则遵循 PHP 的强制类型转换逻辑。
- 使用时需注意浮点数计算可能因 PHP 舍入错误导致意外结果,如 19.99 * 100 返回 1998 而非 1999。
代码示例
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注意事项
- 非整数字符串(如 'number')会返回 0,而负整数字符串(如 '-10')会返回正整数值 10。
- 由于 PHP 浮点数精度问题,涉及浮点运算时可能产生非预期结果,建议在计算前进行精度处理。
- 函数自 WordPress 2.5.0 版本引入,稳定可靠,但需根据具体使用场景考虑数据类型转换的边界情况。
Converts a value to non-negative integer.
Parameters
$maybeintmixedrequired-
Data you wish to have converted to a non-negative integer.
Return
int A non-negative integer.
Source
function absint( $maybeint ) {
return abs( (int) $maybeint );
}
Related
| Used by | Description |
|---|---|
WP_User_Query::get_cache_last_changed()wp-includes/class-wp-user-query.php |
Retrieves the last changed cache timestamp for users and optionally posts. |
WP_REST_Menu_Items_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Prepares a single nav menu item output for response. |
WP_REST_Menu_Items_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Prepares a single nav menu item for create or update. |
WP_REST_Pattern_Directory_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php |
Prepare a raw block pattern before it gets output in a REST API response. |
build_query_vars_from_query_block()wp-includes/blocks.php |
Helper function that constructs a WP_Query args array from a |
get_sitemap_url()wp-includes/sitemaps.php |
Retrieves the full URL for a sitemap. |
get_metadata_raw()wp-includes/meta.php |
Retrieves raw metadata value for the specified object. |
wp_filter_content_tags()wp-includes/media.php |
Filters specific tags in post content and modifies their markup. |
WP_Sitemaps::render_sitemaps()wp-includes/sitemaps/class-wp-sitemaps.php |
Renders sitemap templates based on rewrite rules. |
wp_get_user_request()wp-includes/user.php |
Returns the user request object for the specified request ID. |
wp_validate_user_request_key()wp-includes/user.php |
Validates a user request by comparing the key with the request’s key. |
wp_send_user_request()wp-includes/user.php |
Send a confirmation request email to confirm an action. |
WP_Privacy_Requests_Table::get_views()wp-admin/includes/class-wp-privacy-requests-table.php |
Gets an associative array ( id => link ) with the list of views available on this table. |
WP_Privacy_Requests_Table::prepare_items()wp-admin/includes/class-wp-privacy-requests-table.php |
Prepares items to output. |
_wp_privacy_resend_request()wp-admin/includes/privacy-tools.php |
Resend an existing request and return the result. |
_wp_privacy_completed_request()wp-admin/includes/privacy-tools.php |
Marks a request as completed by the admin and logs the current timestamp. |
_wp_personal_data_handle_actions()wp-admin/includes/privacy-tools.php |
Handle list table actions. |
WP_User::for_site()wp-includes/class-wp-user.php |
Sets the site to operate on. Defaults to the current site. |
WP_Customize_Manager::handle_load_themes_request()wp-includes/class-wp-customize-manager.php |
Loads themes into the theme browsing/installation UI. |
WP_Roles::for_site()wp-includes/class-wp-roles.php |
Sets the site to operate on. Defaults to the current site. |
wpdb::parse_db_host()wp-includes/class-wpdb.php |
Parses the DB_HOST setting to interpret it for mysqli_real_connect(). |
WP_Community_Events::__construct()wp-admin/includes/class-wp-community-events.php |
Constructor for WP_Community_Events. |
WP_Community_Events::cache_events()wp-admin/includes/class-wp-community-events.php |
Caches an array of events data from the Events API. |
WP_Customize_Manager::_validate_header_video()wp-includes/class-wp-customize-manager.php |
Callback for validating the header_video value. |
get_header_video_url()wp-includes/theme.php |
Retrieves header video URL for custom header. |
get_header_video_settings()wp-includes/theme.php |
Retrieves header video settings. |
WP_REST_Users_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php |
Prepares a single user for creation or update. |
WP_REST_Users_Controller::delete_item()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php |
Deletes a single user. |
WP_REST_Comments_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Retrieves a list of comment items. |
WP_Term_Query::parse_query()wp-includes/class-wp-term-query.php |
Parse arguments passed to the term query with default query parameters. |
get_current_network_id()wp-includes/load.php |
Retrieves the current network ID. |
WP_Network_Query::get_network_ids()wp-includes/class-wp-network-query.php |
Used internally to get a list of network IDs matching the query vars. |
wp_localize_jquery_ui_datepicker()wp-includes/script-loader.php |
Localizes the jQuery UI datepicker. |
WP_HTTP_Requests_Response::set_status()wp-includes/class-wp-http-requests-response.php |
Sets the 3-digit HTTP status code. |
WP_Site_Query::get_site_ids()wp-includes/class-wp-site-query.php |
Used internally to get a list of site IDs matching the query vars. |
get_oembed_response_data_rich()wp-includes/embed.php |
Filters the oEmbed response data to return an iframe embed code. |
get_post_embed_html()wp-includes/embed.php |
Retrieves the embed code for a specific post. |
get_oembed_response_data()wp-includes/embed.php |
Retrieves the oEmbed response data for a given post. |
get_header_image_tag()wp-includes/theme.php |
Creates image tag markup for a custom header image. |
wp_get_attachment_image_srcset()wp-includes/media.php |
Retrieves the value for an image attachment’s ‘srcset’ attribute. |
wp_get_attachment_image_sizes()wp-includes/media.php |
Retrieves the value for an image attachment’s ‘sizes’ attribute. |
wp_calculate_image_sizes()wp-includes/media.php |
Creates a ‘sizes’ attribute value for an image. |
_wp_get_image_size_from_meta()wp-includes/media.php |
Gets the image size as array from its meta data. |
WP_HTTP_Response::set_status()wp-includes/class-wp-http-response.php |
Sets the 3-digit HTTP status code. |
WP_Comment_Query::get_comment_ids()wp-includes/class-wp-comment-query.php |
Used internally to get a list of comment IDs matching the query vars. |
wp_handle_comment_submission()wp-includes/comment.php |
Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form. |
WP_Customize_Cropped_Image_Control::to_json()wp-includes/customize/class-wp-customize-cropped-image-control.php |
Refresh the parameters passed to the JavaScript via JSON. |
WP_Customize_Nav_Menus::search_available_items_query()wp-includes/class-wp-customize-nav-menus.php |
Performs post queries for available-item searching. |
WP_Customize_Nav_Menus::ajax_load_available_items()wp-includes/class-wp-customize-nav-menus.php |
Ajax handler for loading available menu items. |
WP_Customize_Nav_Menus::ajax_search_available_items()wp-includes/class-wp-customize-nav-menus.php |
Ajax handler for searching available menu items. |
wp_ajax_crop_image()wp-admin/includes/ajax-actions.php |
Handles cropping an image via AJAX. |
get_avatar_data()wp-includes/link-template.php |
Retrieves default data about the avatar. |
Custom_Background::ajax_background_add()wp-admin/includes/class-custom-background.php |
Handles Ajax request for adding custom background context to an attachment. |
WP_Links_List_Table::prepare_items()wp-admin/includes/class-wp-links-list-table.php |
|
WP_List_Table::get_pagenum()wp-admin/includes/class-wp-list-table.php |
Gets the current page number. |
wp_check_locked_posts()wp-admin/includes/misc.php |
Checks lock status for posts displayed on the Posts screen. |
wp_refresh_post_lock()wp-admin/includes/misc.php |
Checks lock status on the New/Edit Post screen and refresh the lock. |
wp_refresh_post_nonces()wp-admin/includes/misc.php |
Checks nonce expiration on the New/Edit Post screen and refresh if needed. |
wp_dashboard()wp-admin/includes/dashboard.php |
Displays the dashboard. |
add_submenu_page()wp-admin/includes/plugin.php |
Adds a submenu page. |
get_media_item()wp-admin/includes/media.php |
Retrieves HTML form for modifying the image attachment. |
media_upload_form()wp-admin/includes/media.php |
Outputs the legacy media upload form. |
wp_ajax_save_attachment_order()wp-admin/includes/ajax-actions.php |
Handles saving the attachment order via AJAX. |
wp_ajax_get_attachment()wp-admin/includes/ajax-actions.php |
Handles getting an attachment via AJAX. |
wp_ajax_save_attachment()wp-admin/includes/ajax-actions.php |
Handles updating attachment attributes via AJAX. |
wp_ajax_save_attachment_compat()wp-admin/includes/ajax-actions.php |
Handles saving backward compatible attachment attributes via AJAX. |
wp_ajax_wp_link_ajax()wp-admin/includes/ajax-actions.php |
Handles internal linking via AJAX. |
wp_ajax_get_comments()wp-admin/includes/ajax-actions.php |
Handles getting comments via AJAX. |
wp_ajax_replyto_comment()wp-admin/includes/ajax-actions.php |
Handles replying to a comment via AJAX. |
wp_prepare_revisions_for_js()wp-admin/includes/revision.php |
Prepare revisions for JavaScript. |
wp_ajax_autocomplete_user()wp-admin/includes/ajax-actions.php |
Handles user autocomplete via AJAX. |
WP_Comments_List_Table::get_views()wp-admin/includes/class-wp-comments-list-table.php |
|
WP_Comments_List_Table::__construct()wp-admin/includes/class-wp-comments-list-table.php |
Constructor. |
wp_nav_menu_item_post_type_meta_box()wp-admin/includes/nav-menu.php |
Displays a meta box for a post type menu item. |
wp_nav_menu_item_taxonomy_meta_box()wp-admin/includes/nav-menu.php |
Displays a meta box for a taxonomy menu item. |
get_pending_comments_num()wp-admin/includes/comment.php |
Gets the number of pending comments on a post or posts. |
Custom_Image_Header::get_header_dimensions()wp-admin/includes/class-custom-image-header.php |
Calculates width and height based on what the currently selected theme supports. |
Custom_Image_Header::ajax_header_crop()wp-admin/includes/class-custom-image-header.php |
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_add()wp-admin/includes/class-custom-image-header.php |
Given an attachment ID for a header image, updates its “last used” timestamp to now. |
Custom_Image_Header::ajax_header_remove()wp-admin/includes/class-custom-image-header.php |
Given an attachment ID for a header image, unsets it as a user-uploaded header image for the active theme. |
Custom_Image_Header::step_2()wp-admin/includes/class-custom-image-header.php |
Displays second step of custom header image page. |
Custom_Image_Header::step_3()wp-admin/includes/class-custom-image-header.php |
Displays third step of custom header image page. |
Custom_Image_Header::js_2()wp-admin/includes/class-custom-image-header.php |
Displays JavaScript based on Step 2. |
Custom_Background::wp_set_background_image()wp-admin/includes/class-custom-background.php |
|
WP_Customize_Manager::register_controls()wp-includes/class-wp-customize-manager.php |
Registers some default controls. |
sanitize_option()wp-includes/formatting.php |
Sanitizes various option values based on the nature of the option. |
wp_rand()wp-includes/pluggable.php |
Generates a random non-negative number. |
wp_get_archives()wp-includes/general-template.php |
Displays archive links based on type and format. |
WP_Query::get_posts()wp-includes/class-wp-query.php |
Retrieves an array of posts based on query variables. |
WP_Query::parse_query()wp-includes/class-wp-query.php |
Parses a query string and sets query type booleans. |
WP_Query::parse_tax_query()wp-includes/class-wp-query.php |
Parses various taxonomy related query vars. |
get_current_blog_id()wp-includes/load.php |
Retrieves the current site ID. |
get_status_header_desc()wp-includes/functions.php |
Retrieves the description for the HTTP status. |
number_format_i18n()wp-includes/functions.php |
Converts float number to format based on the locale. |
WP_Widget_Recent_Comments::update()wp-includes/widgets/class-wp-widget-recent-comments.php |
Handles updating settings for the current Recent Comments widget instance. |
WP_Widget_Recent_Comments::form()wp-includes/widgets/class-wp-widget-recent-comments.php |
Outputs the settings form for the Recent Comments widget. |
WP_Widget_Recent_Posts::widget()wp-includes/widgets/class-wp-widget-recent-posts.php |
Outputs the content for the current Recent Posts widget instance. |
WP_Widget_Recent_Posts::form()wp-includes/widgets/class-wp-widget-recent-posts.php |
Outputs the settings form for the Recent Posts widget. |
WP_Widget_Recent_Comments::widget()wp-includes/widgets/class-wp-widget-recent-comments.php |
Outputs the content for the current Recent Comments widget instance. |
get_post_comments_feed_link()wp-includes/link-template.php |
Retrieves the permalink for the post comments feed. |
rss_enclosure()wp-includes/feed.php |
Displays the rss enclosure for the current post. |
WP_User_Query::prepare_query()wp-includes/class-wp-user-query.php |
Prepares the query variables. |
wp_video_shortcode()wp-includes/media.php |
Builds the Video shortcode output. |
wp_get_attachment_image()wp-includes/media.php |
Gets an HTML img element representing an image attachment. |
add_image_size()wp-includes/media.php |
Registers a new image size. |
set_post_thumbnail()wp-includes/post.php |
Sets the post thumbnail (featured image) for the given post. |
wp_get_recent_posts()wp-includes/post.php |
Retrieves a number of recent posts. |
is_sticky()wp-includes/post.php |
Determines whether a post is sticky. |
get_post_custom()wp-includes/post.php |
Retrieves post meta fields, based on post ID. |
url_to_postid()wp-includes/rewrite.php |
Examines a URL and try to determine the post ID it represents. |
get_bookmarks()wp-includes/bookmark.php |
Retrieves the list of bookmarks. |
wp_xmlrpc_server::mw_getRecentPosts()wp-includes/class-wp-xmlrpc-server.php |
Retrieves list of recent posts. |
wp_xmlrpc_server::mt_getRecentPostTitles()wp-includes/class-wp-xmlrpc-server.php |
Retrieves the post titles of recent posts. |
wp_xmlrpc_server::blogger_getRecentPosts()wp-includes/class-wp-xmlrpc-server.php |
Retrieves the list of recent posts. |
wp_xmlrpc_server::wp_getMediaLibrary()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a collection of media library items (or attachments). |
wp_xmlrpc_server::wp_newComment()wp-includes/class-wp-xmlrpc-server.php |
Creates a new comment. |
wp_xmlrpc_server::wp_getComments()wp-includes/class-wp-xmlrpc-server.php |
Retrieves comments. |
wp_xmlrpc_server::wp_getPosts()wp-includes/class-wp-xmlrpc-server.php |
Retrieves posts. |
wp_xmlrpc_server::wp_getTerms()wp-includes/class-wp-xmlrpc-server.php |
Retrieves all terms for a taxonomy. |
wp_xmlrpc_server::wp_getUsers()wp-includes/class-wp-xmlrpc-server.php |
Retrieves users. |
wp_xmlrpc_server::_insert_post()wp-includes/class-wp-xmlrpc-server.php |
Helper method for wp_newPost() and wp_editPost(), containing shared logic. |
WP_Customize_Header_Image_Control::enqueue()wp-includes/customize/class-wp-customize-header-image-control.php |
|
WP_Customize_Header_Image_Control::render_content()wp-includes/customize/class-wp-customize-header-image-control.php |
|
wp_new_comment()wp-includes/comment.php |
Adds a new comment to the database. |
delete_metadata()wp-includes/meta.php |
Deletes metadata for the specified object. |
metadata_exists()wp-includes/meta.php |
Determines if a meta field with the given key exists for the given object ID. |
_WP_Editors::wp_link_query()wp-includes/class-wp-editor.php |
Performs post queries for internal linking. |
add_metadata()wp-includes/meta.php |
Adds metadata for the specified object. |
update_metadata()wp-includes/meta.php |
Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Skip to note 5 content
M A Vinoth Kumar
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) // 0Skip to note 6 content
Drew Jaynes
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 ); // 10Skip to note 7 content
Joel James
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 1999Skip to note 8 content
tradesouthwest
As to math and floating numbers there are results to consider when using
absint(). Here are some tests.echo '<ul> <li>0 returns '. absint( 0 ) . '</li> <li>1 returns '. absint( 1 ) . '</li> <li>-1 returns '. absint( -1 ) . '</li> <li>-1 + 1 returns '. absint( (-1 + 1) ) . '</li> <li>1 * -1 returns '. absint( (1 * -1) ) . '</li> <li>1 / -1 returns '. absint( (1 / -1) ) . '</li> <li>1 - 1 returns '. absint( (1 - 1) ) . '</li> <li>0 * -1 returns '. absint( (0 * -1) ) . '</li> <li>0 / 1 returns '. absint( (0 / 1) ) . '</li> <li>0 - 1 returns '. absint( (0 - 1) ) . '</li> <li>0.99 * 1 returns '. absint( (0.99 * 1) ) . '</li> <li>1.99 * 1 returns '. absint( (1.99 * 1) ) . '</li> <li>0.99 / 1 returns '. absint( (0.99 / 1) ) . '</li> <li>1.99 / 1 returns '. absint( (1.99 / 1) ) . '</li> </ul>';Prints:
<br />
0 returns 0<br />
1 returns 1<br />
-1 returns 1<br />
-1 + 1 returns 0<br />
1 * -1 returns 1<br />
1 / -1 returns 1<br />
1 - 1 returns 0<br />
0 * -1 returns 0<br />
0 / 1 returns 0<br />
0 - 1 returns 1<br />
0.99 * 1 returns 0<br />
1.99 * 1 returns 1<br />
0.99 / 1 returns 0<br />
1.99 / 1 returns 1<br />