settype()

Description

This function sets the type of a variable. The data type of a passed variable can be changed at runtime using settype().

Syntax


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

Parameters

&$var
Reference to a variable to convert. This parameter must be specified as a variable and not an expression.
$type
Type to convert &$var to. Possible values are:

  • “boolean”
  • “bool”
  • “integer”
  • “int”
  • “float”
  • “string”
  • “array”
  • “object”
  • “null”

Return Value

bool
A Boolean value is returned as follows:

  • true on success
  • false on failure

Examples

1. Convert Among Scalar Types

This example converts a variable among scalar types. It first assigns $testVal to the float value 3.14159 and calls var_dump() to display the type and value. It then converts the type of $testVal to a string, integer, float, boolean, and null, dumping its type and value each time.

<?php
$testVal = 3.14159;
var_dump( $testVal );

settype( $testVal, "string");
var_dump( $testVal );

settype( $testVal, "integer");
var_dump( $testVal );

settype( $testVal, "float");
var_dump( $testVal );

settype( $testVal, "boolean");
var_dump( $testVal );

settype( $testVal, "null");
var_dump( $testVal );
?>

Here is the output of the program above. The $testVal variable starts out as a float with the value 3.14159. It is then converted to a string which consists of the characters 3.14159. Next, the variable is converted to an integer which lops off the fractional (decimal) part of the float leaving it with the integer value of 3. It is then converted back to float, which just turns the integer 3 into a floating point 3 (note that the lost decimal part is not restored). The variable is then converted to a boolean value. Since all non-zero values are consider true, the value of $testVal is now true. Finally, the variable is converted to a null, which gives it the value of null.

float(3.14159)
string(7) "3.14159"
int(3)
float(3)
bool(true)
NULL

2. Converting String with Leading Digits to Integer

In this example, the $testVar variable is assigned the string value “12three”. It is then converted to an integer with settype().

<?php
$testVar = "12three";
settype( $testVar, "int" );
var_dump( $testVar );
?>

As can be seen from the output below, settype() takes the leading digits of the string as the value for the converted variable.

int(12)

3. Converting String with Leading Characters to Integer

In this example, the $testVar variable is assigned the string value “three21”. It is then converted to an integer with settype().

<?php
$testVar = "three21";
settype( $testVar, "int" );
var_dump( $testVar );
?>

As can be seen from the output below, settype() finds no leading digits and just converts the variable to an integer with value 0.

int(0)

4. Converting String with Leading Floating Point Value to Float

In this example, the $testVar variable is assigned the string value “3.1415garbage32”. It is then converted to a float with settype().

<?php
$testVar = "3.1415garbage32";
$retVal = settype( $testVar, "float" );
var_dump( $testVar );
?>

As can be seen from the output below, settype() takes the leading digits and fractional part of the string as the value for the converted variable. It leaves the garbage and trailing digits behind.

float(3.1415)

5. Convert String to Boolean

This example converts various string variables to Booleans. The $testVal is assigned the values of “true”, “false”, and “” (null string). After each assignment the variable’s type is converted to bool and its type and value examined.

<?php
$testVar = "true";
$retVal = settype( $testVar, "bool" );
var_dump( $testVar );

$testVar = "false";
$retVal = settype( $testVar, "bool" );
var_dump( $testVar );

$testVar = "";
$retVal = settype( $testVar, "bool" );
var_dump( $testVar );
?>

The first value (“true”) is converted to a Boolean and gets the value true. However, the second value (“false”) also gets the value of true. It turns out any non-empty string, when converted to a Boolean, gets the value true. The $testVal variable is then assigned the empty string (“”), which gets the value false when converted to a Boolean variable.

bool(true)
bool(true)
bool(false)

x. Converting to Array

Description of example….



x. Converting to Object

Description of example….



See Also

References