Menu Home

settype

osbo.com | PHP | Functions | 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

<?

$var = false;

var_dump($var);

$type = "integer";

settype($var, $type);

var_dump($var);

?>
bool(false)
int(0)

2

<?

$var = 0;

var_dump($var);

$type = "boolean";

settype($var, $type);

var_dump($var);

?>
int(0)
bool(false)

3

<?

$var = 0;

var_dump($var);

$type = "float";

settype($var, $type);

var_dump($var);

?>
int(0)
float(0)

4

<?

$var = 0.0;

var_dump($var);

$type = "integer";

settype($var, $type);

var_dump($var);

?>
float(0)
int(0)

5

<?

$var = 0.0;

var_dump($var);

$type = "string";

settype($var, $type);

var_dump($var);

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

6

<?

$var = "0.0";

var_dump($var);

$type = "float";

settype($var, $type);

var_dump($var);

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

PHP