• PHP Video Tutorials

PHP - settype() Function



Definition and Usage

The function settype() set or modifies the type of a variable.

Syntax

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

Parameters

Sr.No Parameter Description
1

var

Mandatory. The variable being converted.

2

type

Mandatory. Specifies the type to convert var to.Possibles values of type are:

  • "boolean" or "bool"

  • "integer" or "int"

  • "float" or "double"

  • "string"

  • "array"

  • "object"

  • "null"

Return Values

This function returns true on success or false on failure.

Dependencies

PHP 4 and above

Example

Following example demonstrates use of function 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);
  ?>

Output

This will produce following result −

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