gettype

gettype(mixed $value): string

返回变量类型名字符串(历史名:float 返回 double、int 返回 integer);调试场景推荐 get_debug_type。

C C23

C23
#include <stdio.h>

int main(void) {
    /* C 没有运行时类型查询:类型只存在于编译期(C23 typeof 也是编译期推断)。
       运行时「查类型」只能手写 tag 记录 */
    enum { T_INT, T_DOUBLE, T_STR } tag = T_DOUBLE;
    const char *names[] = {"integer", "double", "string"};
    printf("%s\n", names[tag]);
    return 0;
}
输出
double

C++ C++26

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

int main() {
    // RTTI typeid 比较类型身份;.name() 的拼写由实现定义(GCC 会修饰名),
    // 所以跨平台只比身份、不直接打印名字
    double x = 1.5;
    std::cout << std::boolalpha << (typeid(x) == typeid(double)) << "\n";
    std::cout << (typeid(x) == typeid(1.5)) << "\n";
    return 0;
}
输出
true
true

C# v11.0.100

v11.0.100
// GetType().Name 暴露运行时类型名;float 关键字对应 System.Single
object[] values = { true, 1, 1.5f, 1.5d, "x", new int[] { 1 } };
foreach (object v in values)
{
    Console.WriteLine(v.GetType().Name);
}
输出
Boolean
Int32
Single
Double
String
Int32[]

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// %T 直接打印任意值的运行时类型名
	for _, v := range []any{true, int64(1), 1.5, "x", []int{1}} {
		fmt.Printf("%T\n", v)
	}
}
输出
bool
int64
float64
string
[]int

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // 原始类型不是对象:装箱后才能拿到 getClass()
        Object[] values = { Boolean.TRUE, Integer.valueOf(1), 1.5d, "x", new int[] { 1 } };
        for (Object v : values) {
            System.out.println(v.getClass().getSimpleName());
        }
    }
}
输出
Boolean
Integer
Double
String
int[]

JavaScript v26.13.1

v26.13.1
// typeof 返回 7 种字符串;typeof null === "object" 是历史 bug
const values = [true, 1, 1.5, "x", [1], null];
for (const v of values) {
    console.log(typeof v);
}
console.log(typeof null === "object", Array.isArray([1]));
输出
boolean
number
number
string
object
object
true true

PHP v8.6.0

v8.6.0
// gettype 返回历史名;get_debug_type(PHP 8.0+)返回真实类型名
$values = [true, 1, 1.5, 'x', [1], null, new stdClass()];
foreach ($values as $v) {
    echo str_pad(gettype($v), 9), get_debug_type($v), "\n";
}
输出
boolean  bool
integer  int
double   float
string   string
array    array
NULL     null
object   stdClass

Python v3.15.2

v3.15.2
values = [True, 1, 1.5, "x", [1], None]
for v in values:
    print(type(v).__name__)
# bool 是 int 的子类:isinstance(True, int) 为 True
print(isinstance(True, int))
输出
bool
int
float
str
list
NoneType
True

Rust v1.99.1

v1.99.1
fn type_of<T>(_: &T) -> &'static str {
    std::any::type_name::<T>()
}

fn main() {
    // type_name::<T>() 是编译期泛型字符串(调试辅助;具体拼写不保证稳定)
    println!("{}", type_of(&true));
    println!("{}", type_of(&1i64));
    println!("{}", type_of(&1.5f64));
    println!("{}", type_of(&"x"));
    println!("{}", type_of(&vec![1i32]));
}
输出
bool
i64
f64
&str
alloc::vec::Vec<i32>

TypeScript v26.13.1

v26.13.1
// 运行时 typeof 与 JS 相同;编译期收窄改用类型守卫
const values = [true, 1, 1.5, "x", [1], null];
for (const v of values) {
    console.log(typeof v);
}
function isNumber(v: unknown): v is number {
    return typeof v === "number";
}
console.log(isNumber(1.5), typeof null === "object");
输出
boolean
number
number
string
object
object
true true

语言差异说明

C:没有运行时类型查询——类型只存在于编译期;运行时「查类型」只能手写 tag(本例 tag 枚举)。PHP 返回的历史名 double 正来自 C 的 double 类型。

C++:RTTI 提供 typeid 身份比较,但 .name() 拼写由实现定义(GCC 输出修饰名、clang 输出易读名),跨平台代码只能比较 typeid(x) == typeid(double) 而非打印名字。

C#:v.GetType().Name 直接给运行时名——float 关键字对应 System.Single(打印 Single),double 对应 Double。

Go:fmt.Printf("%T") 一行搞定运行时类型名;float64 呼应 PHP 的历史名 double。

Java:getClass().getSimpleName();原始类型不是对象,装箱(Integer.valueOf)才有运行时类。

JavaScript:typeof 只返回 7 种字符串;typeof null === "object" 是历史 bug,判 null 必须 === null,判数组用 Array.isArray。

PHP:gettype 返回历史名(integer/double/boolean/NULL),与类型声明用的名字(int/float/bool/null)永不相等——签名检查、错误消息拼接都会踩坑;手册定位:类型检查用 is_* 函数,调试取真实类型名用 get_debug_type(PHP 8.0+),本条目两函数对照演示正是这个层级关系。

Python:type(v).__name__ 直接给真实名(float/NoneType);bool 是 int 的子类(isinstance(True, int) 为 True)——动态性比 PHP 更深一层。

Rust:std::any::type_name::<T>() 是编译期泛型字符串(调试辅助,拼写不保证稳定);真运行时动态查询要走 Any trait 的 downcast_ref。

TypeScript:运行时 typeof 与 JS 相同(null bug 一并继承);编译期类型收窄用类型守卫(v is number)或 instanceof。