PHP Function Reference

PHP settype() Function



The PHP settype() function sets the type of the PHP variable. It is used to set type or modify type of an existing variable.

Syntax

settype(variable, type)

Parameters

variable Required. Specify the variable being converted.
type Required. Specify the type of variable that is needed. Possible values for this parameter are:
  • "boolean" or "bool"
  • "integer" or "int"
  • "float" or "double"
  • "string"
  • "array"
  • "object"
  • "null"

Return Value

Returns true on success or false on failure.

Example:

The example below shows the usage of settype() function.

<?php
$x = 32.58;
$y = "48.99";
$z = true;

settype($x, "int");
settype($y, "int");
settype($z, "int");

echo "$x \n";  
echo "$y \n"; 
echo "$z \n"; 
?>

The output of the above code will be:

32 
48 
1 

Example:

Consider one more example to understand this function.

<?php
$x = "Hello123.5";
$y = "123.5Hello";
$z = true;

settype($x, "float");
settype($y, "float");
settype($z, "string");

echo "$x \n";  
echo "$y \n"; 
echo "$z \n"; 
?>

The output of the above code will be:

0 
123.5 
1 

❮ PHP Variable Handling Reference