php 中获取变量类型的函数是 gettype(),它返回一个表示变量类型的字符串。返回的类型字符串可以是:布尔值、整形、浮点值、字符串、数组、对象、资源或 null。

PHP 函数中获取变量类型信息
在 PHP 中,我们可以使用 gettype() 函数来获取变量的类型信息。它返回一个字符串,表示变量的类型。
语法:
立即学习“PHP免费学习笔记(深入)”;
gettype(variable)
参数:
variable:要获取类型信息的变量。返回值:
类型字符串:
| 类型 | 字符串 |
|---|---|
| 布尔值 | boolean |
| 整形 | integer |
| 浮点值 | double |
| 字符串 | string |
| 数组 | array |
| 对象 | object |
| 资源 | resource |
| NULL | NULL |
| 未定义 | undefined |
实战案例:
<?php // 获取布尔值变量的类型 $is_true = true; $type = gettype($is_true); echo "Variable 'is_true' is of type: $type\n"; // 获取整形变量的类型 $number = 123; $type = gettype($number); echo "Variable 'number' is of type: $type\n"; ?>
输出:
Variable 'is_true' is of type: boolean Variable 'number' is of type: integer
以上就是PHP 函数中如何获取变量的类型信息?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号