CKTIS / PHP
settype()
Converte la variabile sul posto in base al nome del tipo.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
settype(mixed &$var, string $type): boolConverte la variabile sul posto in base al nome del tipo. Il booleano restituito indica il successo e il valore convertito resta nel primo argomento. In PHP 8 un nome di tipo sconosciuto genera ValueError.
Firma
settype(mixed &$var, string $type): bool
Output dell’esempio
[true,12,"int"]
ValueError
Esempi
<?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"; }
Risultato
[true,12,"int"] ValueError