settype()

settype() — Set the type of a variable.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

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

Parameters

ParameterTypeWhat it takes
&$varmixedThe variable being converted.
$typestringPossibles values of type are: "boolean" or "bool" "integer" or "int" "float" or "double" "string" "array" "object" "null"

Return value

Always returns true.

Example

A runnable snippet:

$foo = "5bar"; // string $bar = true; // boolean settype($foo, "integer"); // $foo is now 5 (integer) settype($bar, "string"); // $bar is now "1" (string) var_dump($foo, $bar);

Changelog

VersionChange
8.0.0Now throws a ValueError when an invalid type is passed to type. Previously, a E_WARNING was emitted and the function returned false.

See also

gettype()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page settype() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.