Either echo or dump is selected in php according to the type of the variable

  • 2020-05-17 05:03:27
  • OfStack

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

is_scalar -- checks if the variable is a scalar

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

 
<?php 
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 articles: