is_scalar
(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)
is_scalar — 查找变量是否是标量
说明
查找表达式是否作为标量值进行计算。
请参阅标量类型以了解更多信息。
注意:
is_scalar() 不会将 resource 类型值视为标量, 因为当前 resources 是基于整数(integer)的抽象数据类型。 不能依赖该执行细节,因为它可能会改变。
注意:
is_scalar() 不会将 NULL 检测为标量。
参数
value
-
需要检测的变量。
示例
示例 #1 is_scalar() 示例
<?php
function show_var($var)
{
if (is_scalar($var)) {
echo $var;
} else {
var_dump($var);
}
}
$pi = 3.1416;
$proteins = array("hemoglobin", "cytochrome c oxidase", "ferredoxin");
show_var($pi);
show_var($proteins)
?>
以上示例会输出:
3.1416 array(3) { [0]=> string(10) "hemoglobin" [1]=> string(20) "cytochrome c oxidase" [2]=> string(10) "ferredoxin" }
参见
- is_float() - 检测变量是否是浮点型
- is_int() - 检测变量是否是整数
- is_numeric() - 检测变量是否是数字或数字字符串
- is_real() - 别名 is_float
- is_string() - 检测变量的类型是否是字符串
- is_bool() - 检测变量是否是布尔值
- is_object() - 检测变量是否是对象
- is_array() - 检测变量是否是数组
+添加备注
用户贡献的备注 3 notes
Dr K ¶
19 years ago
Having hunted around the manual, I've not found a clear statement of what makes a type "scalar" (e.g. if some future version of the language introduces a new kind of type, what criterion will decide if it's "scalar"? - that goes beyond just listing what's scalar in the current version.)
In other lanuages, it means "has ordering operators" - i.e. "less than" and friends.
It (-:currently:-) appears to have the same meaning in PHP.
Anonymous ¶
18 years ago
Another warning in response to the previous note:
> just a warning as it appears that an empty value is not a scalar.
That statement is wrong--or, at least, has been fixed with a later revision than the one tested. The following code generated the following output on PHP 4.3.9.
CODE:
<?php
echo('is_scalar() test:'.EOL);
echo("NULL: " . print_R(is_scalar(NULL), true) . EOL);
echo("false: " . print_R(is_scalar(false), true) . EOL);
echo("(empty): " . print_R(is_scalar(''), true) . EOL);
echo("0: " . print_R(is_scalar(0), true) . EOL);
echo("'0': " . print_R(is_scalar('0'), true) . EOL);
?>
OUTPUT:
is_scalar() test:
NULL:
false: 1
(empty): 1
0: 1
'0': 1
THUS:
* NULL is NOT a scalar
* false, (empty string), 0, and "0" ARE scalars
efelch at gmail dot com ¶
19 years ago
A scalar is a single item or value, compared to things like arrays and objects which have multiple values. This tends to be the standard definition of the word in terms of programming. An integer, character, etc are scalars. Strings are probably considered scalars since they only hold "one" value (the value represented by the characters represented) and nothing else.