Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / PHP

ValueError

Označava ispravan tip, ali nedopuštenu vrijednost.

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

Sintaksa

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

Označava ispravan tip, ali nedopuštenu vrijednost. Ugrađena funkcija može izazvati ValueError za zabranjen raspon ili opciju i TypeError za nekompatibilan tip. I aplikacija može koristiti ValueError za svoja ograničenja.

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"]

Primjeri

<?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]);

Rezultat

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

Izvori