settypeSET the type of a
VARIABLE.
This function returns TRUE, if the $type is set correctly to the provided VARIABLE.
The possibles values of $type are:
STRING, INTEGER or INT, FLOAT or DOUBLE, BOOLEAN or BOOL, ARRAY, NULL or OBJECT.
This function returns FALSE on failure.
Passed by reference means that the memory address of the variable, (a pointer to the memory location), is passed to the function.
This is unlike passing by value, where the value of a variable is passed on.
<?php
bool settype ( mix &$var , str $type )
where,
$var = The VARIABLE to be converted
( Passed as reference )
$type = The TYPE to set
?>
&$var
The variable to be converted.
Passed by reference.
$type
The type to set.
EXERCISE
<?php
$vl01a = false;
$vl01b = 'string';
$vl01c = 1e36;
$vl01d = null;
$vl01e = TRUE;
$vl01f = imagecreate(1,1);
echo 'Before settype:<br><br>';
var_dump($vl01a);
echo '<br>';
var_dump($vl01b);
echo '<br>';
var_dump($vl01c);
echo '<br>';
var_dump($vl01d);
echo '<br>';
var_dump($vl01e);
echo '<br>';
var_dump($vl01f);
$op01 = mt_rand(1, 2);
if($op01 == 1)
{
$types = [ 'string', 'int', 'integer',
'float', 'double',
'bool', 'boolean',
'array', 'null', 'object' ];
}
else
{
$types = [ 'STRING', 'INT', 'INTEGER',
'FLOAT', 'DOUBLE',
'BOOL', 'BOOLEAN',
'ARRAY', 'NULL', 'OBJECT' ];
}
$x01 = mt_rand(0, count($types) - 1);
$vls01a = settype($vl01a, $types[$x01]);
$vls01b = settype($vl01b, $types[$x01]);
$vls01c = settype($vl01c, $types[$x01]);
$vls01d = settype($vl01d, $types[$x01]);
$vls01e = settype($vl01e, $types[$x01]);
$vls01f = settype($vl01f, $types[$x01]);
echo '<br><br>After settype:<br><br>';
echo $types[$x01] . '<br><br>';
var_dump($vl01a);
echo '<br>';
var_dump($vl01b);
echo '<br>';
var_dump($vl01c);
echo '<br>';
var_dump($vl01d);
echo '<br>';
var_dump($vl01e);
echo '<br>';
var_dump($vl01f);
?>
RESULT
For each new run a new result is obtained.