CKTIS / PHP
Keyword default
Define the fallback entry point of a switch when no case matches.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
default:Define the fallback entry point of a switch when no case matches. It need not be last, and execution can fall through it after a matching case without break. Only one default label is allowed. A match expression also accepts a default arm as an exhaustive fallback.
function pick(int $n): string { $s=""; switch($n){case 1:$s.="A"; default:$s.="D"; break; case 2:$s.="B";} return $s; }
echo json_encode([pick(1),pick(2),pick(8)]);
["AD","B","D"]
Examples
<?php
function pick(int $n): string { $s=""; switch($n){case 1:$s.="A"; default:$s.="D"; break; case 2:$s.="B";} return $s; }
echo json_encode([pick(1),pick(2),pick(8)]);
Result
["AD","B","D"]