Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

PHP type true

The literal type true denotes exactly the boolean value true.

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

Syntax

function accepted(bool $ok): true

The literal type true denotes exactly the boolean value true. Its domain meaning depends on the function, such as indicating that an error was detected. It is available as a standalone type from PHP 8.2. If an operation can also fail, handle that case separately, for example by throwing. Returning false raises TypeError even without 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

Examples

<?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(); }

Result

true
rejected

Sources