php怎么设置变量类型

2021-11-12,

php中,可利用settype()函数来设置变量类型,语法“settype($var,"数据类型")”;数据类型值可为“boolean”、“integer”、“float”、“string”、“array”、“object”、“null”。

/2021/11/92f66cb1.jpg

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

在php中,可利用settype()函数来设置变量类型。

settype() 函数用于设置变量的数据类型。(版本要求: PHP 4, PHP 5, PHP 7)

语法:

settype ( $var ,  $type ) //将变量var类型设置成type类型

$type的可能值为:

  • "boolean" (或为"bool",从 PHP 4.2.0 起)

  • "integer" (或为"int",从 PHP 4.2.0 起)

  • "float" (只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的"double"现已停用)

  • "string"

  • "array"

  • "object"

  • "null" (从 PHP 4.2.0 起)

返回值:设置成功时返回 TRUE, 失败时返回 FALSE。

示例:

<?php
$foo = "5bar"; // string
$bar = true;   // boolean

settype($foo, "integer"); // $foo is now 5   (integer)
settype($bar, "string");  // $bar is now "1" (string)
?>

推荐学习:《PHP视频教程》

以上就是php怎么设置变量类型的详细内容,更多请关注北冥有鱼其它相关文章!

本文转载自【PHP中文网】,希望能给您带来帮助,苟日新、日日新、又日新,生命不息,学习不止。

《php怎么设置变量类型.doc》

下载本文的Word格式文档,以方便收藏与打印。