Sometimes we want to output variables in characters, we may use dump_var, but if we want to output automatically, we need to calculate the function.
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);
// 打印:3.1416
show_var($proteins)
// 打印:
// array(3) {
// [0]=>
// string(10) "hemoglobin"
// [1]=>
// string(20) "cytochrome c oxidase"
// [2]=>
// string(10) "ferredoxin"
// }
?>
|
|
Copy code
|
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"
// }
?>
Description
bool is_scalar ( mixed $var )
is_scalar() returns TRUE if the given variable argument var is a scalar, FALSE otherwise. Scalar variables refer to those variables containing integer, float, string or boolean, while array, object and resource are not scalars.
Note:
Although the current resource types are integers, is_scalar() will not treat them as scalars because resources are abstract data types. Implementation details cannot be relied upon as they may change.
http://www.bkjia.com/PHPjc/629106.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629106.htmlTechArticleSometimes we want to output variables in characters. We may use dump_var, but if we want to output automatically, we need Calculate the function. The code is as follows Copy the code ?php function show_v...