Home >Backend Development >PHP Tutorial >In php, choose echo or dump according to the type of variable

In php, choose echo or dump according to the type of variable

高洛峰
高洛峰Original
2016-12-01 10:11:051047browse

At this point, the is_scalar built-in function comes in handy.

is_scalar -- Check whether a variable is a scalar

Scalar variables refer to those variables that contain integer, float, string or boolean, while array, object and resource are not scalars.

Copy code The code is as follows:
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);
// Print: 3.1416

show_var($proteins)
/ / Print:
// 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