Home  >  Article  >  Backend Development  >  How to determine whether a variable is a scalar using is_scalar in php

How to determine whether a variable is a scalar using is_scalar in php

不言
不言forward
2019-03-09 13:44:532451browse

The content of this article is about how is_scalar in PHP determines whether a variable is a scalar. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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.

<?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); 
// 打印:3.1416 

show_var($proteins) 
// 打印: 
// array(3) { 
// [0]=> 
// string(10) "hemoglobin" 
// [1]=> 
// string(20) "cytochrome c oxidase" 
// [2]=> 
// string(10) "ferredoxin" 
// } 
?>

The above is the detailed content of How to determine whether a variable is a scalar using is_scalar in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete