rest_is_boolean()
云策文档标注
概述
rest_is_boolean() 函数用于判断给定值是否为布尔类型或类似布尔的值。它检查值是否为布尔值、字符串或整数,并返回相应的布尔结果。
关键要点
- 函数接受一个参数 $maybe_bool,可以是布尔值、字符串或整数。
- 如果值是布尔类型,直接返回 true。
- 对于字符串,检查是否在有效布尔值数组('false'、'true'、'0'、'1')中,忽略大小写。
- 对于整数,检查是否为 0 或 1。
- 其他情况返回 false。
代码示例
function rest_is_boolean( $maybe_bool ) {
if ( is_bool( $maybe_bool ) ) {
return true;
}
if ( is_string( $maybe_bool ) ) {
$maybe_bool = strtolower( $maybe_bool );
$valid_boolean_values = array(
'false',
'true',
'0',
'1',
);
return in_array( $maybe_bool, $valid_boolean_values, true );
}
if ( is_int( $maybe_bool ) ) {
return in_array( $maybe_bool, array( 0, 1 ), true );
}
return false;
}
原文内容
Determines if a given value is boolean-like.
Parameters
$maybe_boolbool|stringrequired-
The value being evaluated.
Source
function rest_is_boolean( $maybe_bool ) {
if ( is_bool( $maybe_bool ) ) {
return true;
}
if ( is_string( $maybe_bool ) ) {
$maybe_bool = strtolower( $maybe_bool );
$valid_boolean_values = array(
'false',
'true',
'0',
'1',
);
return in_array( $maybe_bool, $valid_boolean_values, true );
}
if ( is_int( $maybe_bool ) ) {
return in_array( $maybe_bool, array( 0, 1 ), true );
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |