Home  >  Article  >  Backend Development  >  php中根据变量的类型 选择echo或dump_php技巧

php中根据变量的类型 选择echo或dump_php技巧

WBOY
WBOYOriginal
2016-05-17 09:10:28826browse

此时,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"
// }
?>
Statement:
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