Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

settype

Description

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) {
}
HomeMenu