Home  >  Article  >  Backend Development  >  PHP automatically identifies echo or dump according to the type of variable_PHP tutorial

PHP automatically identifies echo or dump according to the type of variable_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:51799browse

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.

show_var($proteins) // Print:
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

// array(3) {

// [0]=>

// string(10) "hemoglobin"

// [1]=> // [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...
Statement:
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