English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP settype() function usage and example

PHP available functions

settype() The function is used to set the type of the variable.

PHP version requirement: PHP 4, PHP 5, PHP 7

Syntax

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

Parameter description:

  • $var: The variable to be converted.

  • $type: The possible values of type are.

    • "boolean" (or "bool", from PHP 4.2Starting from .0)
    • "integer" (or "int", from PHP 4.2Starting from .0)
    • "float" (only in PHP 4.2.0 can be used, for the "double" used in old versions, it is now deprecated)
    • "string"
    • "array"
    • "object"
    • "null" (from PHP 4.2Starting from .0)

Return value

Returns TRUE on success, FALSE on failure.

Online example

<?php
$foo = "5bar"; // string
$bar = true;   // boolean
var_dump($foo);
var_dump($bar);
settype($foo, "integer"); // $foo is now 5   (integer)
settype($bar, "string");  // $bar is now "1(string)
var_dump($foo);
var_dump($bar);
?>

The output is:

string(4) "5bar"
bool(true)
int(5)
string(1) "1"

PHP available functions