settype

Set the type of a variable

Syntax

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

Parameters

var

The variable being converted.

type

Possibles values of type are:

"boolean" or "bool"

"integer" or "int"

"float" or "double"

"string"

"array"

"object"

"null"

Return

Returns true on success or false on failure.

Examples

1 · null to boolean

<?

$var = null;
$type = "boolean";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
NULL
bool(false)

2 · boolean to null

<?

$var = false;
$type = "null";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
bool(false)
NULL

3 · boolean to integer

<?

$var = false;
$type = "integer";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
bool(false)
int(0)

4 · integer to boolean

<?

$var = 0;
$type = "boolean";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
int(0)
bool(false)

5 · integer to float

<?

$var = 0;
$type = "float";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
int(0)
float(0)

6 · float to integer

<?

$var = 0.0;
$type = "integer";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
float(0)
int(0)

7 · float to string

<?

$var = 0.0;
$type = "string";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
float(0)
string(1) "0"

8 · string to float

<?

$var = "0.0";
$type = "float";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
string(3) "0.0"
float(0)

9 · array to object

<?

$var = array();
$type = "object";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
array(0) {
}
object(stdClass)#1 (0) {
}

10 · object to array

<?

$var = new stdclass;
$type = "array";

var_dump($var);

settype($var, $type);

var_dump($var);

?>
object(stdClass)#1 (0) {
}
array(0) {
}

boolval

debug_zval_dump

doubleval

empty

floatval

get_debug_type

get_defined_vars

get_resource_id

get_resource_type

gettype

intval

is_array

is_bool

is_callable

is_countable

is_double

is_float

is_int

is_integer

is_iterable

is_long

is_null

is_numeric

is_object

is_real

is_resource

is_scalar

is_string

isset

print_r

serialize

strval

unserialize

unset

var_dump

var_export

PHP

Home Menu