In this article, you will learn how to convert a variable of one type into another type. The settype() function in PHP converts a variable to a specific type.
what is the syntax of the SETTYPE() function in php?
settype(variable, type);
Parameter | Description |
---|---|
variable | Required. Specifies the variable to convert |
type | Required. Specifies the type to convert variable to. The possible types are: boolean, bool, integer, int, float, double, string, array, object, null |
examples of the SETTYPE() function
Example 1. In this example, we convert variables to specific types.
<?php
$a = "32"; // string
settype($a, "integer"); // $a is now integer
$b = 32; // integer
settype($b, "string"); // $b is now string
$c = true; // boolean
settype($c, "integer"); // $c is now integer (1)
?>