Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

ValueError

Indica un tipo corretto ma un valore inammissibile.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

catch (ValueError $failure) { /* handle invalid value */ }

Indica un tipo corretto ma un valore inammissibile. Una funzione integrata può lanciare ValueError per un intervallo o opzione vietati e TypeError per tipo incompatibile. Anche il codice applicativo può usare ValueError per i propri vincoli.

try { str_repeat("x",-1); } catch(ValueError $e){$bad=get_class($e);}
try { str_repeat([],2); } catch(TypeError $e){$wrong=get_class($e);}
echo json_encode([str_repeat("x",2),$bad,$wrong]);
["xx","ValueError","TypeError"]

Esempi

<?php
try { str_repeat("x",-1); } catch(ValueError $e){$bad=get_class($e);}
try { str_repeat([],2); } catch(TypeError $e){$wrong=get_class($e);}
echo json_encode([str_repeat("x",2),$bad,$wrong]);

Risultato

["xx","ValueError","TypeError"]

Fonti