PHP 中 gettype() 与 PHP 8 中 get_debug_type() 的区别
在早期的 PHP 版本中,如果我们想获取变量类型,通常使用gettype()函数。该函数以字符串形式返回变量类型。它返回所有可能的值,如整数、字符串、数组、布尔值、浮点数、资源、NULL、未知类型等。
但是,gettype函数存在问题。它不返回本机和熟悉的类型名称。它返回浮点数而不是浮点数,返回整数而不是 int 等。
为了解决此问题,PHP 8 使用get_debug_type函数。
get_debug_type() 函数
在 PHP 8 中,get_debug_type 函数返回变量的真实本机类型。它返回浮点数、int 而不是浮点数和整数。此函数自动解析对象类名称。
get_debug_type() 函数有助于
调试
业务逻辑
错误报告
示例: 在 PHP 中使用 gettype() 函数
<?php
class Novel {}
class Comments {}
$novel = new Novel();
if(! ($novel instanceof Comment)) {
echo 'Expected ' . Comment::class . ' still got My' . (is_object($novel) ?
get_class($novel) : gettype($novel));
}
?>输出
Expected Comment still got MyNovel
示例 : 在 PHP 8 中使用 get_debug_type() 函数
<?php
class Novel {}
class Comments {}
$novel = new Novel();
if(! ($novel instanceof Comment)) {
echo 'Expected '.Comment::class.' still got My'.get_debug_type($novel);
}
?>输出
Expected Comment still got MyNovel
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP