CKTIS / PHP
Słowo kluczowe match
Wybiera wynik na podstawie ścisłej identyczności z argumentem.
Wersja: PHP 8.4; repository runtime, production runtime not attested
Składnia
match ($subject) { $key => $result, default => $fallback }Wybiera wynik na podstawie ścisłej identyczności z argumentem. Oblicza tylko wynik pasującego ramienia i nie przechodzi do następnego. Bez zgodnego ramienia i default rzuca UnhandledMatchError. Kilka kluczy może dzielić wynik.
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
Przykłady
<?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);}
Wynik
["int","text"] UnhandledMatchError