Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

TypeError

Identify a value that fails a declared or required type, for instance a wrong argument or return. strict_types suppresses most scalar coercion at a user-function call site.

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

Syntax

catch (TypeError $failure) { /* handle invalid type */ }

Identify a value that fails a declared or required type, for instance a wrong argument or return. strict_types suppresses most scalar coercion at a user-function call site. An integer remains valid for float. TypeError extends Error and can be caught as Throwable.

declare(strict_types=1);
function twice(int $n): int{return $n*2;}
try{twice("2");}catch(TypeError $e){echo json_encode([twice(2),$e instanceof Error,$e instanceof Throwable]);}
[4,true,true]

Examples

<?php
declare(strict_types=1);
function twice(int $n): int{return $n*2;}
try{twice("2");}catch(TypeError $e){echo json_encode([twice(2),$e instanceof Error,$e instanceof Throwable]);}

Result

[4,true,true]

Sources