CKTIS / PHP
Parola chiave match
Sceglie un risultato per identità rigorosa con il soggetto.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
match ($subject) { $key => $result, default => $fallback }Sceglie un risultato per identità rigorosa con il soggetto. Valuta solo il risultato del ramo corrispondente senza passare al seguente. Senza corrispondenza né default lancia UnhandledMatchError. Più chiavi possono condividere un risultato.
function label($v){return match($v){1,2=>"int","1"=>"text"};}
echo json_encode([label(1),label("1")]),PHP_EOL;
try{label(3);}catch(UnhandledMatchError $e){echo get_class($e);}
["int","text"]
UnhandledMatchError
Esempi
<?php
function label($v){return match($v){1,2=>"int","1"=>"text"};}
echo json_encode([label(1),label("1")]),PHP_EOL;
try{label(3);}catch(UnhandledMatchError $e){echo get_class($e);}
Risultato
["int","text"] UnhandledMatchError