Home > Backend Development > PHP Tutorial > 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

不言
Release: 2023-04-05 14:48:01
forward
2497 people have browsed it

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" 
// } 
?>
Copy after login

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!

Related labels:
php
source:csdn.net
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