settype

Views: 2 Last modified: July 31st, 2011 Comments: 0

The function settype() will set a particular function to a specified type. The first parameter must be the variable which needs to be converted and the second parameter must be the type.

Supported types

“boolean” (or, since PHP 4.2.0, “bool”)
“integer” (or, since PHP 4.2.0, “int”)
“float” (only possible since PHP 4.2.0, for older versions use the deprecated variant “double”)
“string”
“array”
“object”
“null” (since PHP 4.2.0)

<?php
$int = 25;
$str = 'Hello world!';
$bool = true;
$arr = array(1,2,3);
$obj = new stdClass;
$str2 = 'Hello World!';

settype($int, 'string');
settype($str, 'array');
settype($bool, 'integer');
settype($arr, 'object');
settype($obj, 'array');
settype($str2, 'null');

var_dump($int);
echo '<br />';
var_dump($str);
echo '<br />';
var_dump($bool);
echo '<br />';
var_dump($arr);
echo '<br />';
var_dump($obj);
echo '<br />';
var_dump($str2);
?>

The result

string(2) “25″
array(1) { [0]=> string(12) “Hello world!” }
int(1)
object(stdClass)#2 (3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
array(0) { }
NULL
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {7+9} =  
    Anything to add ?

        You must be logged in to post a comment.