CKTIS / PHP
Uvjeti i grananje
if pretvara uvjet u bool pa 0 i „0” preskaču istinitu granu. match strogo uspoređuje vrijednost i tip te vrednuje samo odabranu granu.
Verzija: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
if ($value) { /* branch */ }
match ($value) { 0 => "zero", default => "other" };if pretvara uvjet u bool pa 0 i „0” preskaču istinitu granu. match strogo uspoređuje vrijednost i tip te vrednuje samo odabranu granu. Bez podudaranja i default izaziva UnhandledMatchError. Uvjeti se vrednuju redom do prvog podudaranja. Izvršava se samo desni rezultatski izraz odabrane grane.
function classify($v){$branch="no"; if($v){$branch="yes";} return [$branch,match($v){0=>"int","0"=>"text",default=>"other"}];}
echo json_encode([classify(0),classify("0"),classify("00")]);
$trace=[]; $mark=function($tag,$v)use(&$trace){$trace[]=$tag;return $v;}; $r=match(2){$mark("c1",1)=>$mark("r1","A"),$mark("c2",2)=>$mark("r2","B"),$mark("c3",3)=>"C"};echo "\n",json_encode([$r,$trace]);
[["no","int"],["no","text"],["yes","other"]]
["B",["c1","c2","r2"]]
Primjeri
<?php
function classify($v){$branch="no"; if($v){$branch="yes";} return [$branch,match($v){0=>"int","0"=>"text",default=>"other"}];}
echo json_encode([classify(0),classify("0"),classify("00")]);
$trace=[]; $mark=function($tag,$v)use(&$trace){$trace[]=$tag;return $v;}; $r=match(2){$mark("c1",1)=>$mark("r1","A"),$mark("c2",2)=>$mark("r2","B"),$mark("c3",3)=>"C"};echo "\n",json_encode([$r,$trace]);
Rezultat
[["no","int"],["no","text"],["yes","other"]] ["B",["c1","c2","r2"]]