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

高洛峰
Release: 2023-03-02 16:22:02
Original
1069 people have browsed it

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"
// }
?>

Related labels:
php
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template