PHP | gettype()函数

gettype()函数是PHP中的一个内置函数,用于获取变量的类型。它用于检查现有变量的类型。

null

语法:

string gettype ( $var )

参数: 此函数只接受一个参数 $var .需要检查变量类型的变量名称。

返回值: 此函数返回字符串类型的值。返回字符串的可能值为:

  • 布尔值
  • 整数
  • double(由于历史原因,浮动时返回“double”)
  • 一串
  • 大堆
  • 对象
  • 资源
  • 无效的
  • 未知类型

下面的程序演示了PHP中的gettype()函数:

项目1:

<?php
// PHP program to illustrate gettype() function
$var1 = true; // boolean value
$var2 = 3; // integer value
$var3 = 5.6; // double value
$var4 = "Abc3462" ; // string value
$var5 = array (1, 2, 3); // array value
$var6 = new stdClass; // object value
$var7 = NULL; // null value
$var8 = tmpfile(); // resource value
echo gettype ( $var1 ). "" ;
echo gettype ( $var2 ). "" ;
echo gettype ( $var3 ). "" ;
echo gettype ( $var4 ). "" ;
echo gettype ( $var5 ). "" ;
echo gettype ( $var6 ). "" ;
echo gettype ( $var7 ). "" ;
echo gettype ( $var8 ). "" ;
?>


输出:

boolean
integer
double
string
array
object
NULL
resource

项目2:

<?php
// PHP program to illustrate gettype() function
$var1 = "GfG" ;
$var2 = 10 % 7;
$var3 = pow(10, 2);
$var4 = pow(10, 0.5);
$var5 = sqrt(9);
echo gettype ( $var1 ). "" ;
echo gettype ( $var2 ). "" ;
echo gettype ( $var3 ). "" ;
echo gettype ( $var4 ). "" ;
echo gettype ( $var5 );
?>


输出:

string
integer
integer
double
double

参考: http://php.net/manual/en/function.gettype.php

PHP是一种专门为web开发设计的服务器端脚本语言。通过以下步骤,您可以从头开始学习PHP PHP的教程 PHP示例 .

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享