CKTIS / PHP
Keyword finally
Run cleanup after a try or catch during normal completion, return or exception unwinding.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
try { work(); } finally { clean(); }Run cleanup after a try or catch during normal completion, return or exception unwinding. A pending return value is evaluated before cleanup. A return from finally overrides that pending result, and a new exception there supersedes the earlier one. Terminating with exit bypasses finally.
function pick(&$log){try {$log[]="try";return 1;}finally {$log[]="finally";return 2;}}
$log=[]; $value=pick($log);echo json_encode([$value,$log]);
[2,["try","finally"]]
Examples
<?php
function pick(&$log){try {$log[]="try";return 1;}finally {$log[]="finally";return 2;}}
$log=[]; $value=pick($log);echo json_encode([$value,$log]);
Result
[2,["try","finally"]]