PHP 函数中如何从字符串中推断变量类型?(推断.字符串.变量.函数.类型...)

wufei1232024-08-26PHP42

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

PHP 函数中如何从字符串中推断变量类型?

如何在 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 函数中如何从字符串中推断变量类型?的详细内容,更多请关注知识资源分享宝库其它相关文章!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。