CKTIS / PHP
settype()
Convert the variable in place using the named target type.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
settype(mixed &$var, string $type): boolConvert the variable in place using the named target type. The returned boolean reports success, and the converted value stays in the first argument. An unsupported type name raises ValueError in PHP 8.
Signature
settype(mixed &$var, string $type): bool
Example output
[true,12,"int"]
ValueError
Examples
<?php
$quantity = "12";
$ok = settype($quantity, "int");
echo json_encode([$ok, $quantity, get_debug_type($quantity)]), PHP_EOL;
try { settype($quantity, "decimal"); } catch (ValueError $e) { echo "ValueError"; }
Result
[true,12,"int"] ValueError