Home >Backend Development >PHP Tutorial >In php, choose echo or dump according to the type of variable
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"
// }
?>