- var_dump: documentation ( source)
- settype: documentation ( source)
- is_scalar: documentation ( source)
- is_array: documentation ( source)
- is_object: documentation ( source)
<?php
namespace X;
$val = '1 ';
$type = 'double';
function convertType(& $val, $type)
{
if ($val === NULL || is_object($val)) {
// ignore
} elseif ($type === 'array') {
if (!is_array($val)) {
return FALSE;
}
} elseif (!is_scalar($val)) {
return FALSE;
} elseif ($type !== 'NULL') {
$old = $val = ($val === FALSE ? '0' : (string) $val);
settype($val, $type);
if ($old !== ($val === FALSE ? '0' : (string) $val)) {
return FALSE; // data-loss occurs
}
}
return TRUE;
}
var_dump(convertType($val, $type));
var_dump($val);