首页  >  文章  >  后端开发  >  PHP 获取类型()

PHP 获取类型()

WBOY
WBOY原创
2024-08-29 12:48:46333浏览

PHP 推出了 gettype 函数,用于获取给定变量的类型。无论我们需要获取什么变量的类型,我们都使用 PHP 中的 gettype() 函数。 PHP 中定义的所有内容、每个变量都有一些类型,通过了解该变量的类型,我们可以轻松定义流程或处理该特定变量的功能。 PHP gettype() 函数的用途相同。该版本是在 PHP 4.0+ 稳定版本之后发布的,现在广泛用于编程目的。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

gettype() 函数的语法是:

String gettype(variable_name)

它接受单个参数作为需要查找其类型的输入。

函数的返回类型是输入函数持续存在的可能数据类型。

一些返回值是:

  • 布尔值.: 仅两个值(true/False)
  • 整数.: 所有整数
  • 数组: 数组值
  • 双精度: 十进制值
  • 对象: 类型对象
  • 空:空数据
  • 资源
  • 未知类型

gettype() 函数的工作原理

gettype() 接受一个参数作为参数,我们需要找出其数据类型,取出后,它与定义的数据类型相匹配,以匹配用户作为输入给出的特定数据类型。表是在内部存储空间中定义的,它是给定类型的数据结构。类型匹配,结果作为输出返回。

匹配的数据类型作为特定函数的输出返回。

代码:

Gettype('PHP')
  • PHP 获取该值并在容器中检查映射对象的数据类型,因为它是 String 类型。
  • 返回一个字符串作为输出。

PHP gettype() 示例

下面给出的是 PHP gettype() 的示例:

示例#1:整数

这给出了函数内给定变量的 Integer 类型。

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$a = 3;
echo gettype($a) . "<br>";
$a = 4;
echo gettype($a) . "<br>";
$a = 5;
echo gettype($a) . "<br>";
$a = 6;
echo gettype($a) . "<br>";
?>
</body>
</html>

此示例 PHP 代码返回 Integer 作为输出,因为给定变量的类型为 Integer。

输出:

PHP 获取类型()

示例#2:布尔值

这给出了函数内给定变量的布尔类型。

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$a = false;
echo gettype($a) . "<br>";
$a = true;
echo gettype($a) . "<br>";
?>
</body>
</html>

此示例 PHP 代码返回布尔值作为输出,因为给定变量的类型为布尔值。

输出:
PHP 获取类型()

示例#3:数组

这给出了函数内给定变量的类型数组。

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$a = array();
echo gettype($a) . "<br>";
?>
</body>
</html>

此示例 PHP 代码返回数组作为输出,因为给定变量属于数组类型。

输出:
PHP 获取类型()

我们还可以将值放入数组中并评估结果。

示例#4:双倍

这给出了函数内给定变量的 Double 类型。

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$b = 3.2;
echo gettype($b) . "<br>";
$b = 6.2;
echo gettype($b) . "<br>";
$b = 8.2;
echo gettype($b) . "<br>";
?>
</body>
</html>

此示例 PHP 代码返回 Double 作为输出,因为给定变量的类型为 Double。

输出:

PHP 获取类型()

示例 #5:对象

这给出了函数内给定变量的类型 Object。

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$var6 = new stdClass;
echo gettype($var6)
?>
</body>
</html>

此示例 PHP 代码返回 Object 作为输出,因为给定变量的类型为 Object。

输出:

PHP 获取类型()

示例 #6:空

这给出了函数内给定变量的类型 Null。

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$f = NULL;
echo gettype($f) . "<br>";
$f = NULL;
echo gettype($f) . "<br>";
?>
</body>
</html>

此示例 PHP 代码返回 Null 作为输出,因为给定变量的类型为 Null。

输出:

PHP 获取类型()

示例#7:资源

这给出了函数内给定变量的资源类型。

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$f = tmpfile();
echo gettype($f) . "<br>";
?>
</body>
</html>

此示例 PHP 代码返回 Resource 作为输出,因为给定变量的类型为 Resource。

输出:

PHP 获取类型()

We can also check the type of a variable by creating an Array with Random Datasets.

A foreach loop in PHP works like that.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$arr = array(210, 2.39, new stdClass, "hello,PHP", true);
foreach ($arr as $value) {
echo gettype($value). "<br>" ;
}
?>
</body>
</html>

A sample Array element with some random data type is created and looped in with the gettype function in the above code.

Output:

PHP 获取类型()

Conclusion

From the above article, we saw the use of Function gettype in PHP. We tried to see how the gettype() function works in PHP and what are is use at the programming level from various example and classification. We also saw the internal working and the advantages of having the type of data we define for various programming purposes. Also, the syntax and examples helped us to see much precisely over the function.

以上是PHP 获取类型()的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP ucwords()下一篇:PHP References