可以通过使用 gettype() 函数从字符串中推断变量类型。该函数返回一个字符串,其中包含变量的类型:字符串类型整数类型浮点数类型布尔类型数组类型对象类型

如何在 PHP 函数中从字符串中推断变量类型?
在 PHP 中,可以通过使用 gettype() 函数从字符串中推断变量类型。此函数返回一个字符串,其中包含变量的类型。
<?php
// 字符串类型
$name = "John Doe";
echo gettype($name); // 输出:string
// 整数类型
$age = 30;
echo gettype($age); // 输出:integer
// 浮点数类型
$salary = 1234.56;
echo gettype($salary); // 输出:double
// 布尔类型
$is_active = true;
echo gettype($is_active); // 输出:boolean
// 数组类型
$fruits = ["apple", "banana", "orange"];
echo gettype($fruits); // 输出:array
// 对象类型
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "Jane Doe";
$person->age = 25;
echo gettype($person); // 输出:object
?>以上就是PHP 函数中如何从字符串中推断变量类型?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号