array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.is-scalar.php', 1 => 'is_scalar', ), 'up' => array ( 0 => 'ref.var.php', 1 => '变量处理函数', ), 'prev' => array ( 0 => 'function.is-resource.php', 1 => 'is_resource', ), 'next' => array ( 0 => 'function.is-string.php', 1 => 'is_string', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/var/functions/is-scalar.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)
is_scalar — 检测变量是否是一个标量
检测变量是否为标量
标量变量是指 int、float、string 或 bool 类型的变量。 array、object 、 resource 和 null 类型不是标量。
注意:
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" }