Select echo or dump according to the type of variable in php_PHP tutorial

WBOY
Release: 2016-07-21 15:17:28
Original
883 people have browsed it

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

is_scalar -- Check whether the variable is a scalar

Scalar variables refer to those variables containing 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"
// }
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325696.htmlTechArticleAt this point, the is_scalar built-in function comes in handy. is_scalar -- Check whether a variable is a scalar. Scalar variables are those that contain integer, float, string or boolean, and...
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