php中,可利用settype()函數來設置變量類型,語法“settype($var,"數據類型")”;數據類型值可為“boolean”、“integer”、“float”、“string”、“array”、“object”、“null”。
本教程操作環境: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視頻教程》