Heim  >  Artikel  >  Backend-Entwicklung  >  php中根据变量的类型 选择echo或dump_php技巧

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

WBOY
WBOYOriginal
2016-05-17 09:10:28857Durchsuche

此时,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"
// }
?>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn