RandomCompat_intval() - WordPress 函数参考中文文档

返回介绍

RandomCompat_intval()

发布于 2017-09-11 字数1901 浏览 947 评论 0

RandomCompat_intval( int|float $number,  boolean $fail_open = false )

Cast to an integer if we can, safely.


描述

If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) (non-inclusive), it will sanely cast it to an int. If you it’s equal to ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats lose precision, so the <= and => operators might accidentally let a float through.


参数

$number

(int|float)
(Required)
The number we want to convert to an int

$fail_open

(boolean)
(Optional)
Set to true to not throw an exception

Default value: false


返回值

(int) (or float if $fail_open)


源代码

File: wp-includes/random_compat/cast_to_int.php

    function RandomCompat_intval($number, $fail_open = false)
    {
        if (is_numeric($number)) {
            $number += 0;
        }

        if (
            is_float($number)
            &&
            $number > ~PHP_INT_MAX
            &&
            $number < PHP_INT_MAX
        ) {
            $number = (int) $number;
        }

        if (is_int($number) || $fail_open) {
            return $number;
        }

        throw new TypeError(
            'Expected an integer.'
        );
    }

Collapse full 源代码 code

View on Trac


相关函数

Used By

  • wp-includes/random_compat/random_int.php:
    random_int()
  • wp-includes/random_compat/random_bytes_dev_urandom.php:
    random_bytes()

User Contributed Notes

上一篇:query_posts()

下一篇:RandomCompat_strlen()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!