get_debug_type

get_debug_type(mixed $value): string

返回变量的真实类型名(PHP 8.0+):float/int/stdClass,与 gettype 的历史名 double/integer 形成对照——调试场景一律选它。

C C23

C23
#include <stdio.h>

int main(void) {
    /* C 的类型只活在编译期——运行期问不到类型名。
       唯一可问的运行期问题是 sizeof。 */
    printf("%zu\n", sizeof(1.5)); /* 8:double 的存储尺寸 */
    printf("%zu\n", sizeof(1));   /* 4:sizeof(int) */
    printf("%zu\n", sizeof("x")); /* 2:含 NUL 的 2 字符数组 */
    return 0;
}
输出
8
4
2

C++ C++26

C++26
#include <iostream>
#include <typeinfo>

int main() {
    // typeid 给出运行期名字,但名字由实现定义:
    // gcc/clang 输出 Itanium 重整短名(d = double,i = int)
    std::cout << typeid(1.5).name() << "\n"; // d
    std::cout << typeid(1).name() << "\n";   // i
    std::cout << typeid("x").name() << "\n"; // A2_c:2 字符数组
    return 0;
}
输出
d
i
A2_c

C# v11.0.100

v11.0.100
object[] values = { 1.5, 1, "x", new int[] { } };
foreach (object v in values) {
    Console.WriteLine(v.GetType().Name); // GetType():对实例的运行期查询
}
Console.WriteLine(typeof(double).Name); // typeof:对类型的编译期查询
输出
Double
Int32
String
Int32[]
Double

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	fmt.Printf("%T\n", 1.5)              // float64——Go 直接说出机器类型
	fmt.Printf("%T\n", 1)                // int
	fmt.Printf("%T\n", "x")              // string
	fmt.Printf("%T\n", []int{1})         // []int
	fmt.Printf("%T\n", map[string]int{}) // map[string]int
	fmt.Printf("%T\n", struct{ A int }{}) // struct { A int }:匿名布局同样直说
}
输出
float64
int
string
[]int
map[string]int
struct { A int }

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        Object[] values = {1.5, 1, "x", new int[]{}, null};
        for (Object v : values) {
            // getClass():运行期查询;基本类型没有对象可问
            System.out.println(v == null ? "null" : v.getClass().getSimpleName());
        }
        System.out.println(double.class.getName()); // 基本类型的类字面量
    }
}
输出
Double
Integer
String
int[]
null
double

JavaScript v26.13.1

v26.13.1
console.log(typeof 1.5);      // "number":整数与浮点共用一个类型
console.log(typeof "x");      // "string"
console.log(typeof true);     // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null);     // "object":著名的 typeof bug
const real = (v) => Object.prototype.toString.call(v).slice(8, -1);
console.log(real(1.5)); // "Number"——精确的内部类型名
console.log(real(null)); // "Null"
console.log(real([1]));  // "Array"
输出
number
string
boolean
undefined
object
Number
Null
Array

PHP v8.6.0

v8.6.0
// gettype 保留历史名;get_debug_type 返回现代类型名
var_dump(gettype(1.5));        // double:继承自 PHP 3 的名字
var_dump(get_debug_type(1.5)); // float:真实类型名
var_dump(gettype(1));          // integer
var_dump(get_debug_type(1));   // int
var_dump(get_debug_type('x')); // string
var_dump(get_debug_type(null)); // null
var_dump(get_debug_type(new stdClass())); // 对象返回类名
$r = fopen('php://memory', 'r');
var_dump(get_debug_type($r));  // 资源:"resource (类型)"
fclose($r);
var_dump(get_debug_type($r));  // 已关闭:"resource (closed)"
输出
string(6) "double"
string(5) "float"
string(7) "integer"
string(3) "int"
string(6) "string"
string(4) "null"
string(8) "stdClass"
string(17) "resource (stream)"
string(17) "resource (closed)"

Python v3.15.2

v3.15.2
print(type(1.5).__name__)    # float——Python 从没把 float 叫过 double
print(type(1).__name__)      # int——任意精度,不是机器字
print(type("x").__name__)    # str
print(type(None).__name__)   # NoneType
print(type([1]).__name__)    # list
print(type(len).__name__)    # builtin_function_or_method
print(type(3 + 2j).__name__) # complex
输出
float
int
str
NoneType
list
builtin_function_or_method
complex

Rust v1.99.1

v1.99.1
use std::any::type_name;

fn main() {
    // 编译期类型名:type_name 在编译期求值,返回完整路径
    println!("{}", type_name::<f64>());      // f64
    println!("{}", type_name::<i32>());      // i32
    println!("{}", type_name::<String>());   // alloc::string::String
    println!("{}", type_name::<Vec<i32>>()); // alloc::vec::Vec<i32>
}
输出
f64
i32
alloc::string::String
alloc::vec::Vec<i32>

TypeScript v26.13.1

v26.13.1
console.log(typeof 1.5);      // "number":整数与浮点共用一个类型
console.log(typeof "x");      // "string"
console.log(typeof true);     // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null);     // "object":著名的 typeof bug
const real = (v: unknown) => Object.prototype.toString.call(v).slice(8, -1);
console.log(real(1.5)); // "Number"——精确的内部类型名
console.log(real(null)); // "Null"
console.log(real([1]));  // "Array"
输出
number
string
boolean
undefined
object
Number
Null
Array

语言差异说明

C:类型只存在于编译期——运行期问不到类型名,唯一可问的是 sizeof(示例以字节尺寸代答);要「带类型的值」只能自己造 tagged union。

C++:typeid(...).name() 是运行期查询,但名字实现相关:gcc/clang 按 Itanium ABI 重整输出 d/i,MSVC 输出 double/int;gcc 下可用 __cxa_demangle 还原可读名。

C#:GetType().Name 查实例(Int32 短名)、typeof(T).Name 查类型,两者都是一等公民;FullName 给全名。

Go:%T / reflect.TypeOf 输出真实类型,连匿名 struct 布局、泛型实例、map/slice 复合类型都如实打印——类型是静态事实,不是运行期装出来的。

Java:getClass().getSimpleName() 只对对象有效;基本类型没有 getClass,用类字面量 double.class;数组类名形如 int[];getSimpleName 只给短名,getName 给全名。

JavaScript:typeof 太粗——typeof null === "object" 是历史 bug,整数浮点共用 number;精确内部名用 Object.prototype.toString.call(x).slice(8, -1)(示例)。

PHP:gettype 保留历史名(double/integer),get_debug_type(8.0+)返 float/int 与类型声明一致;对象返回类名、资源返回 resource (类型)、已关闭资源返回 resource (closed)——调试与错误信息场景一律选 get_debug_type。

Python:type(x).__name__ 即真实名——Python 从来没把 float 叫过 double;且鸭子类型文化里精确类型检查本就不常用。

Rust:std::any::type_name::<T>() 编译期求值、返回完整路径(如 alloc::string::String),官方明确注明仅限调试展示、不保证输出格式稳定。

TypeScript:运行期同 JavaScript;另有编译期 typeof x 类型操作符——同名不同物,类型层与值层各有一个 typeof。