Home > php教程 > php手册 > php中根据变量的类型 选择echo或dump

php中根据变量的类型 选择echo或dump

WBOY
Release: 2016-06-13 11:59:15
Original
1185 people have browsed it

此时,is_scalar内置函数就派上用场了。

is_scalar -- 检测变量是否是一个标量

标量变量是指那些包含了 integer、float、string 或 boolean的变量,而 array、object 和 resource 则不是标量。

复制代码 代码如下:


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);
// 打印:3.1416

show_var($proteins)
// 打印:
// array(3) {
// [0]=>
// string(10) "hemoglobin"
// [1]=>
// string(20) "cytochrome c oxidase"
// [2]=>
// string(10) "ferredoxin"
// }
?>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template