Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

Tipo PHP true

Da PHP 8.2 il tipo letterale true esprime che una chiamata terminata normalmente restituisce esattamente il booleano true.

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

Sintassi

function accepted(bool $ok): true

Da PHP 8.2 il tipo letterale true esprime che una chiamata terminata normalmente restituisce esattamente il booleano true. La funzione può segnalare un insuccesso con un’eccezione. Restituire false produce un TypeError anche senza strict_types.

declare(strict_types=1);
function accepted(bool $ok): true { if (!$ok) { throw new RuntimeException('rejected'); } return true; }
echo json_encode(accepted(true)), PHP_EOL;
try { accepted(false); } catch (RuntimeException $e) { echo $e->getMessage(); }
true
rejected

Esempi

<?php
declare(strict_types=1);
function accepted(bool $ok): true { if (!$ok) { throw new RuntimeException('rejected'); } return true; }
echo json_encode(accepted(true)), PHP_EOL;
try { accepted(false); } catch (RuntimeException $e) { echo $e->getMessage(); }

Risultato

true
rejected

Fonti