PHP - settype() 函式



定義和用法

settype() 函式設定或修改變數的型別。

語法

bool settype ( mixed &$var , string $type )

引數

序號 引數 描述
1

var

必填。要轉換的變數。

2

type

必填。指定將 var 轉換成的型別。type 的可能值為

  • "boolean" 或 "bool"

  • "integer" 或 "int"

  • "float" 或 "double"

  • "string"

  • "array"

  • "object"

  • "null"

返回值

此函式在成功時返回 true,失敗時返回 false

依賴項

PHP 4 及更高版本

示例

以下示例演示了 settype() 函式的使用方法。

  <?php
  $a = "tutorialspoint";
  $b = true;
  echo "***Before settype****<br>";
  var_dump($a);
  echo "<br>";
  var_dump($b);
  echo "<br>";
  settype($a, "integer"); // $a is now 0  (integer)
  settype($b, "string");  // $b is now "1" (string)
  echo "***After settype****<br>";
  var_dump($a);
  echo "<br>";
  var_dump($b);
  ?>

輸出

這將產生以下結果:

  ***Before settype****
  string(14) "tutorialspoint"
  bool(true)
  ***After settype****
  int(0)
  string(1) "1"
廣告